Applet error - Null pointer exception?

The applet loads fine the first time I navigate to the page.

Upon refresh, i get java.lang.NullPointer exception.

Here is my java console information:

basic: Loading applet ...

basic: Initializing applet ...

basic: Starting applet ...

java.lang.NullPointerException

at JAppletTrial_1.populate_combobox(JAppletTrial_1.java:136)

at JAppletTrial_1.start(JAppletTrial_1.java:61)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

basic: Exception: java.lang.NullPointerException

Exception in thread "thread applet-JAppletTrial_1.class" java.lang.NullPointerException

at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)

at sun.plugin.AppletViewer.showAppletException(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

My applet, populate combobox function looks like this:

public void populate_combobox(){

try {

ResultSet rs = selectStmt.executeQuery();

while(rs.next()) {

cboClass obj1 = new cboClass(rs.getString(2), rs.getString(3), rs.getString(1));

this.jcboPerson.addItem(obj1);

}

} catch(SQLException e) {

e.printStackTrace();

}

Like i say, it runs fine the first time. Refreshes will work after a while. Maybe some thread times out after a while? How do I make it work every time?

[1459 byte] By [joeasdfaa] at [2007-10-2 23:10:28]
# 1
So which is line 136?
sabre150a at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 2
136 is the line that has the class stuff in it.cboClass obj1 = new cboClass(rs.getString(2), rs.getString(3), rs.getString(1));Message was edited by: joeasdfa
joeasdfaa at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 3

> 136 is the line that has the class stuff in it.

>

> cboClass obj1 = new cboClass(rs.getString(2),

> rs.getString(3), rs.getString(1));

>

No it isn't.

I don't like the way you fetch the columns out of sequence (it is preferable to fetch the columns in sequence)

But that's not the line.

ChienBerta at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 4
Whoops, perhaps you are right.As i re-ran it, the line that gave the error wasResultSet rs = selectStmt.executeQuery();I still don't know what it would mean?Thanks.
joeasdfaa at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 5

As i re-ran it, the line that gave the error was

ResultSet rs = selectStmt.executeQuery();

I still don't know what it would mean?

Well, NullPointerException means you're trying to use a reference (it should really be NullReferenceException) which is null.

There is only one reference on that line which you're using.

So, somewhere you are setting selectStmt to null and not setting it again before you reuse it.

Do you have some code in your init() method which should be in your start() method?

itchyscratchya at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 6

My init only contains the default init constructed by Netbeans for the applet.

My start method looks like this:

public void start() {

try {

start_statements();

populate_combobox();

hide_panels();

// Initializing GrCapture.

grFinger.initializeCapture(this);

addToTextArea("**Fingerprint reader has successfully initialized**");

} catch (GrErrorException e) {

// write error to log

addToTextArea(e.getMessage()+"**cannot initialize for capture**");

}

}

here is my start_statements function:

public void start_statements(){

try {

// Initializes GrFinger DLL and all necessary utilities.

grFinger = new GrFinger();

Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/health";

dbConnection = DriverManager.getConnection(url,"www", "www");

selectStmt = dbConnection.prepareStatement("Select PersonID, FirstName, LastName from tblperson");

//FingerID will be determined from checkboxes.

enrollStmt = dbConnection.prepareStatement("INSERT INTO tblbiometrics(template, PersonID, FingerID) values(?, ?, ?)");

insertedIdStmt = dbConnection.prepareStatement("SELECT MAX(FingerID) FROM tblbiometrics");

enrollUpdateStmt = dbConnection.prepareStatement("UPDATE tblbiometrics SET template = ? WHERE PersonID=? and FingerID=?");

} catch (GrErrorException e) {

// write error to log

addToTextArea(e.getMessage()+ "GR ERROR EXCEPTION");

} catch (ClassNotFoundException e){

addToTextArea(e.getMessage()+ "Class not found");

} catch (SQLException e){

addToTextArea(e.getMessage()+"SQL ERROR");

}

}

I am setting the select statement before using it. Could it have something to do with the mysql connection? Does it have to be closed in my stop method or something?

joeasdfaa at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...
# 7

Okay, turns out it only works the first time in applet viewer too. When I reload it, the debugger indicates that the object instantiation is throwing an error.

GrFinger = new GrFinger;

It's an SDK, so I don't have access to that code.

When my stop method executes, it throws an error NullPointerException because I am trying to finalize the GrFinger object which never got instantiated. I have tried the obj instantiation in both the start and the init() methods. Like i say it runs the first time. Is there some destructor that I have to call on the close method? or destroy method?

here's my init()

public void init() {

try {

java.awt.EventQueue.invokeAndWait(new Runnable() {

public void run() {

initComponents();

}

});

} catch (Exception ex) {

ex.printStackTrace();

}

try {

grFinger = new GrFinger(); //error throwing here?

} catch (GrErrorException e) {

addToTextArea(e.getMessage()+ "GR ERROR EXCEPTION - Cannot instantiate");

}

}

joeasdfaa at 2007-7-14 6:24:30 > top of Java-index,Java Essentials,New To Java...