Solved

java.lang.NullPointerException

Posted on 2004-08-16
9
250 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is a "java.lang.System Property"   ? 20 67
oracle 11g 23 84
configure dependency in POM for new database 3 26
arguments to jar 5 26
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 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