Solved

java.lang.NullPointerException

Posted on 2004-08-16
9
259 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
ejb wildfly example 2 25
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
collection output issue 9 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 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