getting a null pointer, object is most definatly instantiated

J

Justin

Below is the code for my class:

package orthopedicnotegenerator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.text.Collator;


class DxPrEdit extends JDialog implements ActionListener {

DefaultListModel diagnosisListModel;
String diagnosisIndex[][];
JList diagnosisList;
JScrollPane diagnosisScroller;
String sSN;

DataConnection dataConnection = new DataConnection();

public DxPrEdit(String stringSSN, String selectedDiagnosisInput) {

sSN = stringSSN;

setModal(true);
Container pane = this.getContentPane();
this.setAlwaysOnTop(true);
pane.setLayout(null);
addWidgets();

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(300,100,700,500);

populateDxList(selectedDiagnosisInput);

setVisible(true);
validate();

diagnosisList.setSelectedValue(selectedDiagnosisInput, true);
}

public void addWidgets(){


diagnosisListModel = new DefaultListModel();
JList diagnosisList = new JList(diagnosisListModel);
diagnosisScroller = new JScrollPane(diagnosisList);

JLabel dxHeader = new JLabel("Diagnoses");
dxHeader.setFont(new Font("Serif", Font.BOLD, 30));
add(dxHeader);
dxHeader.setBounds(10,10, dxHeader.getPreferredSize().width,
dxHeader.getPreferredSize().height);

add(diagnosisScroller);
diagnosisScroller.setBounds(10,60, 500, 100);

}

public void populateDxList(String selectedEntry){
diagnosisListModel.removeAllElements();
diagnosisIndex = null;

String sql = "SELECT diagnosisID, exam_NewID,
exam_New_ProblemsID, relatedDiagnosisID, diagnosis, onsetDate, active,
" +
"prePost, iCD FROM diagnoses WHERE sSN=\"" + sSN + "\"
ORDER BY active;";

DefaultListModel queryResults =
dataConnection.queryConnection(sql);

diagnosisIndex = new String[queryResults.getSize() / 9][9];

int counter = 0;

for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < 9; j++){
diagnosisIndex[j] =
String.valueOf(queryResults.getElementAt(counter++));
}
}

Collator myCollator = Collator.getInstance();
for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < diagnosisIndex.length - 1; j++){
if( myCollator.compare(diagnosisIndex[j][6],
diagnosisIndex[j + 1][6]) > 0 ){

String temp[] = new String[9];

temp = diagnosisIndex[j];
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;

}
}
}

for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < diagnosisIndex.length - 1; j++){
if(diagnosisIndex[j][6].equals("Active") &&
diagnosisIndex[j + 1][6].equals("Active")){
if(
myCollator.compare(diagnosisIndex[j][5],diagnosisIndex[j + 1][5]) > 0
){

String temp[] = new String[9];

temp = diagnosisIndex;
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;
}
}

else if(diagnosisIndex[j][6].equals("Inactive") &&
diagnosisIndex[j + 1][6].equals("Inactive")){
if(
myCollator.compare(diagnosisIndex[j][5],diagnosisIndex[j + 1][5]) < 0
){

String temp[] = new String[9];

temp = diagnosisIndex;
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;
}
}

}
}

for(int i = 0; i < diagnosisIndex.length; i++){
diagnosisListModel.addElement(diagnosisIndex[6] + " - "
+ diagnosisIndex[5] + " - " + diagnosisIndex[8] + " "
+ " " + diagnosisIndex[4] );
}

diagnosisScroller.validate();

}

public void updateDx(){

}

public void actionPerformed(ActionEvent ae) {

dispose();
}


//*********************************************************************************

//*********************************************************************************

//*********************************************************************************
public static void main(String args[]){
DxPrEdit dxpr = new DxPrEdit("123-45-6789", "");
}

}

DataConnection is a class that connects to my database. It runs
queries returning the results in a DefaultListModel. The
diagnosisListModel is then populated by different elements of the
returned records. And, in turn, the JList, diagnosisList, is passed
diagnosisListModel to provide the selectable elements.

I am running into a problem when I try to set the selected object in
the JList. It is giving me a null pointer exception at the line where
I attempt to assign a value to the JList.

I also noticed, as I'm playing with it more, that the null pointer is
occuring as I close the dialog box.

As always, any and all help is greatly appreciated.
 
P

Patricia Shanahan

Justin wrote:
....
class DxPrEdit extends JDialog implements ActionListener {

DefaultListModel diagnosisListModel;
String diagnosisIndex[][];
JList diagnosisList; ....
public void addWidgets(){


diagnosisListModel = new DefaultListModel();
JList diagnosisList = new JList(diagnosisListModel); .....
DataConnection is a class that connects to my database. It runs
queries returning the results in a DefaultListModel. The
diagnosisListModel is then populated by different elements of the
returned records. And, in turn, the JList, diagnosisList, is passed
diagnosisListModel to provide the selectable elements.

I am running into a problem when I try to set the selected object in
the JList. It is giving me a null pointer exception at the line where
I attempt to assign a value to the JList.

I also noticed, as I'm playing with it more, that the null pointer is
occuring as I close the dialog box.

As always, any and all help is greatly appreciated.

I could not find any assignments initializing the field diagnosisList.

The local variable diagnosisList in addWidgets is initialized with a
non-null value. Unqualified references to diagnosisList inside
addWidgets refer to the local variable, and should work.

Unqualified references to diagnosisList outside addWidgets refer to the
field, which is null.

If you don't intend to have a separate local variable diagnosisList
inside addWidgets, delete the "JList " from the start of its
declaration, turning it into an assignment to the field.

Patricia
 
H

hiwa

Justin said:
Below is the code for my class:

package orthopedicnotegenerator;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.text.Collator;


class DxPrEdit extends JDialog implements ActionListener {

DefaultListModel diagnosisListModel;
String diagnosisIndex[][];
JList diagnosisList;
JScrollPane diagnosisScroller;
String sSN;

DataConnection dataConnection = new DataConnection();

public DxPrEdit(String stringSSN, String selectedDiagnosisInput) {

sSN = stringSSN;

setModal(true);
Container pane = this.getContentPane();
this.setAlwaysOnTop(true);
pane.setLayout(null);
addWidgets();

setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(300,100,700,500);

populateDxList(selectedDiagnosisInput);

setVisible(true);
validate();

diagnosisList.setSelectedValue(selectedDiagnosisInput, true);
}

public void addWidgets(){


diagnosisListModel = new DefaultListModel();
JList diagnosisList = new JList(diagnosisListModel);
diagnosisScroller = new JScrollPane(diagnosisList);

JLabel dxHeader = new JLabel("Diagnoses");
dxHeader.setFont(new Font("Serif", Font.BOLD, 30));
add(dxHeader);
dxHeader.setBounds(10,10, dxHeader.getPreferredSize().width,
dxHeader.getPreferredSize().height);

add(diagnosisScroller);
diagnosisScroller.setBounds(10,60, 500, 100);

}

public void populateDxList(String selectedEntry){
diagnosisListModel.removeAllElements();
diagnosisIndex = null;

String sql = "SELECT diagnosisID, exam_NewID,
exam_New_ProblemsID, relatedDiagnosisID, diagnosis, onsetDate, active,
" +
"prePost, iCD FROM diagnoses WHERE sSN=\"" + sSN + "\"
ORDER BY active;";

DefaultListModel queryResults =
dataConnection.queryConnection(sql);

diagnosisIndex = new String[queryResults.getSize() / 9][9];

int counter = 0;

for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < 9; j++){
diagnosisIndex[j] =
String.valueOf(queryResults.getElementAt(counter++));
}
}

Collator myCollator = Collator.getInstance();
for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < diagnosisIndex.length - 1; j++){
if( myCollator.compare(diagnosisIndex[j][6],
diagnosisIndex[j + 1][6]) > 0 ){

String temp[] = new String[9];

temp = diagnosisIndex[j];
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;

}
}
}

for(int i = 0; i < diagnosisIndex.length; i++){
for(int j = 0; j < diagnosisIndex.length - 1; j++){
if(diagnosisIndex[j][6].equals("Active") &&
diagnosisIndex[j + 1][6].equals("Active")){
if(
myCollator.compare(diagnosisIndex[j][5],diagnosisIndex[j + 1][5]) > 0
){

String temp[] = new String[9];

temp = diagnosisIndex;
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;
}
}

else if(diagnosisIndex[j][6].equals("Inactive") &&
diagnosisIndex[j + 1][6].equals("Inactive")){
if(
myCollator.compare(diagnosisIndex[j][5],diagnosisIndex[j + 1][5]) < 0
){

String temp[] = new String[9];

temp = diagnosisIndex;
diagnosisIndex[j] = diagnosisIndex[j + 1];
diagnosisIndex[j + 1] = temp;
}
}

}
}

for(int i = 0; i < diagnosisIndex.length; i++){
diagnosisListModel.addElement(diagnosisIndex[6] + " - "
+ diagnosisIndex[5] + " - " + diagnosisIndex[8] + " "
+ " " + diagnosisIndex[4] );
}

diagnosisScroller.validate();

}

public void updateDx(){

}

public void actionPerformed(ActionEvent ae) {

dispose();
}


//*********************************************************************************

//*********************************************************************************

//*********************************************************************************
public static void main(String args[]){
DxPrEdit dxpr = new DxPrEdit("123-45-6789", "");
}

}

DataConnection is a class that connects to my database. It runs
queries returning the results in a DefaultListModel. The
diagnosisListModel is then populated by different elements of the
returned records. And, in turn, the JList, diagnosisList, is passed
diagnosisListModel to provide the selectable elements.

I am running into a problem when I try to set the selected object in
the JList. It is giving me a null pointer exception at the line where
I attempt to assign a value to the JList.

I also noticed, as I'm playing with it more, that the null pointer is
occuring as I close the dialog box.

As always, any and all help is greatly appreciated.

It's a number one newbie error. Local variables, of same names,
hides class fields. Then, class fields remain null.
 
M

Mike Schilling

Your problem is at this line:

JList diagnosisList = new JList(diagnosisListModel);

This creates a local variable called diagnosisList and set its value to a
new JList. You want

diagnosisList = new JList(diagnosisListModel);

to set the value of the *field* diagnosisList.
 
W

Wesley Hall

It's a number one newbie error. Local variables, of same names,
hides class fields. Then, class fields remain null.

....combined with the number one 'newbie' compounding of the problem,
assuming the compiler made a mistake as per "object is most definatly
instantiated".

If you are getting an NPE then you ARE... A) attempting to deference a
null or B) passing a null to a method which later attempts to deference
it. I have been coding in Java a long time and I have never seen a
compiler make a mistake on this, I have seen many programmers convince
themselves the compiler made a mistake though (and I have been guilty of
this also). All the time you are blaming the compiler is wasted time in
in finding and fixing the bug.

The stacktrace is your friend, it will guide you straight to the
problem, and if you want others to help you find the problem, please
include it in your posts :)

Wesley Hall
 
W

Wesley Hall

Wesley said:
...combined with the number one 'newbie' compounding of the problem,
assuming the compiler made a mistake as per "object is most definatly
instantiated".

If you are getting an NPE then you ARE... A) attempting to deference a
null or B) passing a null to a method which later attempts to deference
it. I have been coding in Java a long time and I have never seen a
compiler make a mistake on this, I have seen many programmers convince
themselves the compiler made a mistake though (and I have been guilty of
this also). All the time you are blaming the compiler is wasted time in
in finding and fixing the bug.

The stacktrace is your friend, it will guide you straight to the
problem, and if you want others to help you find the problem, please
include it in your posts :)

Wesley Hall


Errata:

I probably should have said "dont blame the VM" rather than "dont blame
the compiler", in this case as we are talking about null references.
Same thing applies.

Just thought I had better make that correction, before a collegue sees
this post and points it out to me :)

Wesley Hall
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top