Results 1 to 12 of 12
Thread: NullPointerException
- 01-01-2012, 07:41 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
NullPointerException
i hav read other posts regarding the same prob as tht of mine, but i really didnt understand whts wrong in my code. pls help me.. thnk u..
.gif)
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.*; import java.sql.*; class mainframe extends db { JFrame mframe=new JFrame("Current Inventory"); int row_cnt=0; Object data[][]; void create_mframe() throws Exception { JDialog p=new JDialog(); p.setUndecorated(true); p.setResizable(false); p.setSize(200,100); p.setLocationRelativeTo(null); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); p.add(dbl1); p.setVisible(true); con=DriverManager.getConnection("jdbc:odbc:Mangesh"); p.setVisible(false); p.add(dbl2); p.setVisible(true); st=con.createStatement(); p.setVisible(false); p.add(dbl3); p.setVisible(true); p.dispose(); mframe.setSize(pasframe.xSize/2,pasframe.ySize/2); mframe.setLocationRelativeTo(null); mframe.setExtendedState(JFrame.MAXIMIZED_BOTH); Image img=pasframe.tk.getImage("SamratPlast_logo.jpg"); mframe.setIconImage(img); JMenuBar mbar=new JMenuBar(); JMenu menu=new JMenu("Menu"); JMenu option=new JMenu("Option"); JMenu help=new JMenu("Help"); JMenu arrange=new JMenu(" Arrange"); JMenuItem item1=new JMenuItem(" Add new..",new ImageIcon("add.png")); item1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { new mydialog2("Add Item",mframe); } }); JMenuItem item2=new JMenuItem(" Delete",new ImageIcon("delete.png")); JMenuItem item3=new JMenuItem(" Exit",new ImageIcon("exit.png")); item3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }); item3.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { int key=ke.getKeyCode(); if(key==KeyEvent.VK_ENTER) System.exit(0); } }); JMenuItem item5=new JMenuItem(" Search..",new ImageIcon("zoom.png")); JMenuItem item6=new JMenuItem(" About..",new ImageIcon("about.png")); item6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { JLabel ta=new JLabel(); ta.setText("<html><b><font size='7' face='Goudy Old Style'><strong> Inventory Management System</strong></font></b><br> Version 1.0<br> � Mangesh Ghotage , Disha Takalkar<br><br><hr><br> This product is made only for <u>academic purposes</u>.</html>"); JDialog dialog=new JDialog(mframe,"Information",true); dialog.add(new JScrollPane(ta)); dialog.setSize(500,180); //dialog.setLayout(null); dialog.setLocationRelativeTo(null); dialog.setResizable(false); dialog.setVisible(true); } }); item6.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { int key=ke.getKeyCode(); if(key==KeyEvent.VK_ENTER) { JLabel ta=new JLabel(); ta.setText("<html><b><font size='7' face='Goudy Old Style'><strong> Inventory Management System</strong></font></b><br> Version 1.0<br> � Mangesh Ghotage , Disha Takalkar<br><br><hr><br> This product is made only for <u>academic purposes</u>.</html>"); JDialog dialog=new JDialog(mframe,"Information",true); dialog.add(new JScrollPane(ta)); dialog.setSize(500,180); //dialog.setLayout(null); dialog.setLocationRelativeTo(null); dialog.setResizable(false); dialog.setVisible(true); } } }); JMenuItem item7=new JMenuItem(" Reports",new ImageIcon("report.png")); JMenuItem item8=new JMenuItem(" Clear selection..",new ImageIcon("clear.png")); JMenuItem item9=new JMenuItem(" Change password",new ImageIcon("change.png")); item9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { new change_password(); } }); item3.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent ke) { int key=ke.getKeyCode(); if(key==KeyEvent.VK_ENTER) new change_password(); } }); ButtonGroup grp=new ButtonGroup(); JRadioButtonMenuItem aitem1=new JRadioButtonMenuItem(" By Name",new ImageIcon("name.png")); JRadioButtonMenuItem aitem2=new JRadioButtonMenuItem(" By Date",new ImageIcon("date.png")); aitem2.setSelected(true); grp.add(aitem1); grp.add(aitem2); arrange.add(aitem1); arrange.add(aitem2); menu.add(item1); menu.add(item2); menu.add(item8); menu.add(item3); option.add(item5); option.add(arrange); option.add(item9); help.add(item6); help.add(item7); mbar.add(menu); mbar.add(option); mbar.add(help); mframe.setJMenuBar(mbar); mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String title[]={/*"checkbox",*/"Item ID",/*"Update status",*/"Name","Price","Stock"}; rs=st.executeQuery("select * from item"); while(rs.next()){row_cnt++;} System.out.println(row_cnt); System.out.println(con); System.out.println(st); System.out.println(rs); while(rs.next()) { for(int i=0;i<row_cnt;i++) { data[i][0]=rs.getInt(1); data[i][1]=rs.getString(2); data[i][2]=rs.getInt(3); data[i][3]=rs.getInt(4); } } //DefaultTableModel model=new DefaultTableModel(temp,title); JTable table=new JTable(data,title); table.setShowVerticalLines(false); JScrollPane scrollpane=new JScrollPane(table); //table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setRowHeight(20); table.getTableHeader().setFont(new Font("",Font.BOLD,15)); mframe.add(scrollpane); mframe.setVisible(true); System.out.println("sdas"); } } { static Statement st; static Connection con; static ResultSet rs; static ResultSetMetaData rsmd; JLabel dbl1=new JLabel("driver loaded."); JLabel dbl2=new JLabel("connection created."); JLabel dbl3=new JLabel("statement created."); } class execute { public static void main(String a[])throws Exception { new mainframe().create_mframe(); } }Last edited by pbrockway2; 01-01-2012 at 08:34 AM. Reason: code tags corrected
- 01-01-2012, 07:58 AM #2
Re: NullPointerException
Nobody's going to wade through that plethora of unformatted code to find your mistake. I would add the code tags for you, but your non-standard code formatting, with closing braces at the end of code lines, doesn't encourage me to.
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
Better still, step through the code with a debugger and find the error yourself.
And when seeking help with an error, post the exact stack trace. The lines you highlighted don't look likely to have generated a NPE. And what do you mean by
The stack trace will tell you where the exception came from. There's no 'or' about it.the exception is occuring on line no.79 or 99.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-01-2012, 08:12 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: NullPointerException
im new here & this is my 1st post. i hav shortened the code. pls help me
- 01-01-2012, 08:51 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: NullPointerException
Hi mangesh.gho, I've corrected the code tags. What you have to do is put [code] at the start of the code and [/code] at the end.
As you can see using tabs to indent code is not such a good idea! I would recommend using spaces and also keep the lines short. It makes a huge difference especially when code is read in a web browser. Like Darryl I won't read the code in the form its in (it's towards the end of the day here).
Also when you have a runtime error, copy and post the entire stack trace. (the thing which says what line the error occurred on).
Generally null pointer exceptions occur when you use something as if it had a non null value when it is null in actual fact. These can be variables, arrays or things that were returned by method calls.
Use System.out.println() to check values:Java Code:String foo; String[] array; foo.length(); // Bad! foo is null array[0] = foo; // Bad! array is null array = new String[42]; array[0] = foo; array[0].length(); // Bad! array[0] is null someMethod().length(); // might be bad if someMethod() returned null
If someMethod() returns null the System.out.println() will indicate that just before the null pointer exception happens.Java Code:String foo = someMethod(); System.out.println("About to get the length of foo=" + foo); foo.length();
-----
If you get stuck post the actual runtime stack trace. And the code it refers to (formatted in a way so we can read it ;)
- 01-01-2012, 11:33 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: NullPointerException
ohh!! i understood my mistake.. thnk u very much pbrockway2. it ws very helpful info.
- 01-01-2012, 09:19 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: NullPointerException
You're welcome.
- 01-19-2012, 05:21 PM #7
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
Re: NullPointerException
kindly help me to fix it . i am learning java now . i found nullpointerexception when i tried to do matrix addition ie choice : 3. i declared the array matr in beginning of the class.
Ouput with error:Java Code:void addMatrices() throws IOException { getMatA(); getMatB(); if(flaga==0 && flagb==0) { matr= new int[s][s]; System.out.println("\n--------------------Add Matrices Module ---------------------\n"); try { for(i=0;i<s;i++) { for(j=0;j<s;j++) { matr[i][j] = mata[i][j] + matb[i][j]; } } } catch(Exception e) { System.out.println("\tError : "+ e); } //n=3; displayMatR();}
what do you want to do ?
Press the number to start an operation.......
1. Simple Matrix Method - that gets a matrix and displays the
2. Add all elements of a matrix.
3. Add Two given Matrices.
4. Difference between two Matrices.
5. Multiplying Two Matrices.
6. Summing Upper Matrix.
7. Summing Lower Matrix.
8. Summing Diagonal Matrix.
9. Exit.
Choice :
3
************* Getting Input : Matrix A *************
Enter no of rows:
2
Enter no of cols:
2
Enter elements for 2 by 2 Matrix:
1
2
3
4
Values are stored in Matrix A
************* Getting Input : Matrix B *************
Enter no of rows:
2
Enter no of cols:
2
Enter elements for 2 by 2 Matrix:
4
3
2
1
Values are stored in Matrix B
--------------------Add Matrices Module ---------------------
Error : java.lang.NullPointerException
***********Addition Of A and B Matrices**********
0
0
0
0Last edited by j_arif123; 01-19-2012 at 05:26 PM.
- 01-19-2012, 07:25 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: NullPointerException
Did you read post #4? And do what it suggested - ie use System.out.println() to determine the values of variables or expressions that could be null? If so, what happened?
With double arrays there are more things that could be null. For example it is possible for any of matr, matr[i] or matr[i][j] to be null and all need to be checked.
- 01-19-2012, 07:26 PM #9
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
Re: NullPointerException
kindly help me to fix it . i am learning java now . i found nullpointerexception when i tried to do matrix addition ie choice : 3. i declared the array matr in beginning of the class.
Ouput with error:Java Code:void addMatrices() throws IOException { getMatA(); getMatB(); if(flaga==0 && flagb==0) { matr= new int[s][s]; System.out.println("\n--------------------Add Matrices Module ---------------------\n"); try { for(i=0;i<s;i++) { for(j=0;j<s;j++) { matr[i][j] = mata[i][j] + matb[i][j]; } } } catch(Exception e) { System.out.println("\tError : "+ e); } //n=3; displayMatR();}
Error : java.lang.NullPointerException
***********Addition Of A and B Matrices**********
0
0
0
0
- 01-19-2012, 07:32 PM #10
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
Re: NullPointerException
thanks for your response. i will check it.
- 01-19-2012, 07:59 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: NullPointerException
Great - there are six things to check on that line. Once you have found which one is null (and one of them will be) then you have to go back to where you thought you had given it a nonnull value and find out why that didn't happen.
That will be in some other code (that you didn't post). If you get stuck, post the relevant code. But, please, don't use this thread: start your own thread for your own question as this will keep things tidy.
- 01-21-2012, 06:19 AM #12
Member
- Join Date
- Jan 2012
- Location
- India
- Posts
- 32
- Rep Power
- 0
Re: NullPointerException
when i commented the line No: 11 , it showed nothing and no exception occured. i think the problem is in this statement. is it illegal in java that number of elements assigned to a n array dynamically? (like in C or C++ we do it using malloc() function and new operator) Or is there any other methods to do this job? Or do you want to me to post entire code? i could not narrow down the problem.
Similar Threads
-
NullPointerException
By kai555 in forum JDBCReplies: 10Last Post: 08-25-2011, 11:18 AM -
NullPointerException help!
By LoViNgHeArTy in forum New To JavaReplies: 6Last Post: 05-18-2011, 10:24 AM -
NullPointerException
By sanu in forum Java AppletsReplies: 3Last Post: 08-21-2010, 08:37 PM -
NullPointerException
By speedzojie@gmail.com in forum New To JavaReplies: 5Last Post: 06-03-2010, 05:39 PM -
NullPointerException
By JohnST in forum New To JavaReplies: 13Last Post: 01-06-2010, 12:37 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks