[JAVA] NullpointerException?

Pagina: 1
Acties:
  • 46 views sinds 30-01-2008

  • WiseGuy!
  • Registratie: Juni 2003
  • Laatst online: 21:40
Hallo,

Ik krijg bij het uitvoeren van deze Servlet-code een NullPointer terug...

Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package testservlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;

public class KlantServlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html";

  //Initialize global variables
  public void init() throws ServletException {
  

  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    // Variabelen declareren, databaseconnectie opzetten
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    Database db = new Database();
    ResultSet uitkomst = null;
    uitkomst = db.queryUitvoerenSelectKlanten();
    String getafspraak = request.getParameter("klant_id");

    out.println("<HTML><BODY BGCOLOR='#333333'><FONT FACE='VERDANA' SIZE='1'>");

    // Alle bekende klanten tonen
   try{
     while (uitkomst.next()) {
       out.println("<a href='/WebModule1/klantservlet?getafspraak=1&klant_id=" + uitkomst.getInt("klant_id") + "'>" + uitkomst.getString("klant_naam") + "</a>");
     }
   }
   catch (Exception ex) {

     System.out.println("Fout bij ophalen van klanten");
   }
   
   // Alle afspraken van geselecteerde klant tonen
   if(getafspraak.equals("1")){
     
     out.println("ophalen die afspraken");
     
   }
   
   out.println("</FONT></BODY></HTML>");

  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}

class Database {

        Connection con;
        Statement sql;
        ResultSet resultaat;

        public Database() {
        
          // JDBC DRIVERS LADEN
        
          try {
        
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        
          }
          catch (ClassNotFoundException cnfe) {
        
            System.out.println("Class not found");
        
          }
        
          // CONNECTIE MET DATABASE OPZETTEN
        
          try {
        
            con = DriverManager.getConnection("jdbc:odbc:test", "", "");
        
          }
          catch (SQLException sqle) {
        
            System.out.println("Database niet gevonden");
        
          }
        }
        
        public ResultSet queryUitvoerenSelectKlanten() {
          
          try {

                    resultaat = sql.executeQuery("SELECT klant_id, klant_naam FROM klanten;"); 

                }
          catch (SQLException ex) {

                        System.out.println("SQL Fout select");
                        System.out.println("SQLException: " + ex.getMessage()); 

          }

          return resultaat;

        }

}


Het gaat mis op de regel uitkomst = db.queryUitvoerenSelectKlanten();

Ik snap er geen reet van!

we only make way for the man who boldly pushes past us


  • Feyd-Rautha
  • Registratie: November 2001
  • Laatst online: 02-08-2025
Is dit alles wat wij krijgen van informatie op uw probleem op te lossen...

PS: dit is ook niet de manier van werken op GoT... Post de relevante code eens want met zo'n lap code heb ik geen zin dit te doorlopen.

ps: wij zijn ook geen debugger-clubje

[ Voor 14% gewijzigd door Feyd-Rautha op 25-04-2004 21:07 ]

I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. Where the fear has gone there will be nothing. Only I will remain.


  • WiseGuy!
  • Registratie: Juni 2003
  • Laatst online: 21:40
Java:
1
2
3
Database db = new Database();
ResultSet uitkomst = null;
uitkomst = db.queryUitvoerenSelectKlanten();


Hij geeft dus een nullpointer op de laatste van deze 3 regels...

Hier de code van queryUitvoerenSelectKlanten:

Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        public ResultSet queryUitvoerenSelectKlanten() {
          
          try {

                    resultaat = sql.executeQuery("SELECT klant_id, klant_naam FROM klanten;"); 

                }
          catch (SQLException ex) {

                        System.out.println("SQL Fout select");
                        System.out.println("SQLException: " + ex.getMessage()); 

          }

          return resultaat;

        }


Er staan wél gegevens in de database en de query loopt goed, want er worden geen excepties gegooid... Waarom krijg ik dan toch nog een nullpointer?

we only make way for the man who boldly pushes past us


  • Feyd-Rautha
  • Registratie: November 2001
  • Laatst online: 02-08-2025
uw variable sql is natuurlijk nog niet geinitialiseerd.

met
code:
1
Statement sql;


is de variable 'sql' nog steeds gelijk aan null.

Wanneer je daar dan de method 'executeQuery' op loslaat heeft hij dus natuurlijk een nullpointer-exception aangezien 'sql' gelijk is aan null...

>> Statement sql = new Statement is de oplossing

I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. Where the fear has gone there will be nothing. Only I will remain.


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 15:56

.oisyn

Moderator Devschuur®

Demotivational Speaker

Mja
P&W FAQ - De "quickstart"
P&W FAQ - Leer debuggen!!

Debuggen is toch echt iets wat je helemaal zelf mag doen, niet wij.

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Dit topic is gesloten.