Solved

java.lang.NullPointerException

Posted on 2004-08-16
9
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:expertmb
ID: 11808577
>>when i press the JButton nothing happens.....
what is expected when button is pressed?
 
0
 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

705 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