Solved

java.lang.NullPointerException

Posted on 2004-08-16
9
233 Views
Last Modified: 2010-03-31
I'm having a problem with some code here, I keep getting a java.lang.NullPointerException after I call a GUI and click on the JButton to bring me to the code to work on the information I'm after inserting into the GUI. Hopefully someone can help me with this. Would it be because I have the GUI's defined as static, should I define them as something else??? Some code is given below..


public class addtodb extends javax.swing.JFrame
{                  
    static addNewClient anc;
    static assignNeb an;
    public addtodb(){}      
    public void writedb()
    {           
//This is where java.lang.NullPointerException comes up        
        anc.insertbut.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
           {
         try{                              
        try{      
            String query = "SELECT gmsid, sname, fname, Address, dob, gpname FROM   clients";                                    
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");                             Connection conn = DriverManager.getConnection("jdbc:odbc:nebs");
           Statement stmt = conn.createStatement();      
           ResultSet rs = stmt.executeQuery(query);
           int cnt = 0;
           while (rs.next()){
                cnt++;                                    
           }
           cnt = cnt+1;                                    
           System.out.print(anc.gmstxt.getText());
           String sql = "INSERT INTO clients (gmsid, fname, sname, address,gpname, dob)    VALUES ('"+anc.gmstxt.getText()+"','"+anc.fntxt.getText()+"','"+anc.sntxt.getText()+"','"+anc.addtxt.getText()+"','"+anc.gptxt.getText()+"','"+anc.dobtxt.getText()+"')";
           stmt.executeUpdate(sql);      
           try {                                                      anc.dispose();
                      an = new assignNeb();
                an.show();                                             } catch (Exception ex1) {
                          JOptionPane.showMessageDialog(an,"Sorry there is no match for that G.M.S. Number.", "Please Re-Enter.",JOptionPane.ERROR_MESSAGE);
                   }    
            conn.close();                                          
      }
           catch(Exception ex){}            
      }catch(Exception exe){}
            });
      }

}
0
Comment
Question by:cutjack
  • 4
  • 4
9 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 350 total points
ID: 11808495
> static addNewClient anc;

You have to initialize first in order to use it:

static addNewClient anc = new addNewClient();
0
 

Author Comment

by:cutjack
ID: 11808556
You are right, that has got rid of my Exception but you don't know why now when i press the JButton nothing happens.....
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808569
Does it enter the actionPerformed method? Put a System.out.println() statement and tell us if you see the message.
0
 
LVL 6

Expert Comment

by:expertmb
ID: 11808577
>>when i press the JButton nothing happens.....
what is expected when button is pressed?
 
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:cutjack
ID: 11808614
Yeah i put a System.out.println() statement in the actionPerformed method and it didn't appear. I have the button defined as public as well so it should be workin...
0
 

Author Comment

by:cutjack
ID: 11808674
the following should be called

addtodb atb = new addtodb();
atb.writedb();

It should insert the information i entered in the GUI into a database, the was entering it before I tried joining all the different classes and GUI's.....
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808693
It should have worked since you pass the reference of the jbutton in the method. If it does not work there must be something that we are missing. What happens if you move the actionPerformed method in the actual class that you define the method?
0
 

Author Comment

by:cutjack
ID: 11808870
I'm after putting a main method into the class and it works ok from there, thanks for the help but it might be something wrong with another class i'm workin with...
0
 
LVL 35

Expert Comment

by:girionis
ID: 11808901
Thank you for accepting, glad I was of help :)

>  it might be something wrong with another class i'm workin with...

It is always good to separate the GUI from the logic so what I would suggest would be to have your GUi classes separate and maybe write a few other listener classes (implements ActionListener) and put all the database logic there.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now