Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java.lang.NullPointerException

Posted on 2004-08-16
9
Medium Priority
?
280 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 1400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 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