Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By pbrockway2
  • 2 Post By pbrockway2

Thread: NullPointerException

  1. #1
    mangesh.gho is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default 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..

    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>&nbsp;&nbsp;Inventory Management System</strong></font></b><br>&nbsp;&nbsp;&nbsp;Version 1.0<br>&nbsp;&nbsp;� Mangesh Ghotage , Disha Takalkar<br><br><hr><br>&nbsp;&nbsp;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>&nbsp;&nbsp;Inventory Management System</strong></font></b><br>&nbsp;&nbsp;&nbsp;Version 1.0<br>&nbsp;&nbsp;� Mangesh Ghotage , Disha Takalkar<br><br><hr><br>&nbsp;&nbsp;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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default 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 exception is occuring on line no.79 or 99.
    The stack trace will tell you where the exception came from. There's no 'or' about it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    mangesh.gho is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException

    im new here & this is my 1st post. i hav shortened the code. pls help me

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

    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
    Use System.out.println() to check values:

    Java Code:
    String foo = someMethod();
    System.out.println("About to get the length of foo=" + foo);
    foo.length();
    If someMethod() returns null the System.out.println() will indicate that just before the null pointer exception happens.

    -----

    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 ;)
    mangesh.gho likes this.

  5. #5
    mangesh.gho is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: NullPointerException

    ohh!! i understood my mistake.. thnk u very much pbrockway2. it ws very helpful info.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: NullPointerException

    You're welcome.

  7. #7
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default 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.
    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();}
    Ouput with error:
    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
    0
    Last edited by j_arif123; 01-19-2012 at 05:26 PM.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  9. #9
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default 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.
    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();}
    Ouput with error:

    Error : java.lang.NullPointerException

    ***********Addition Of A and B Matrices**********

    0
    0


    0
    0

  10. #10
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: NullPointerException

    thanks for your response. i will check it.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    DarrylBurke and j_arif123 like this.

  12. #12
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default 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

  1. NullPointerException
    By kai555 in forum JDBC
    Replies: 10
    Last Post: 08-25-2011, 11:18 AM
  2. NullPointerException help!
    By LoViNgHeArTy in forum New To Java
    Replies: 6
    Last Post: 05-18-2011, 10:24 AM
  3. NullPointerException
    By sanu in forum Java Applets
    Replies: 3
    Last Post: 08-21-2010, 08:37 PM
  4. NullPointerException
    By speedzojie@gmail.com in forum New To Java
    Replies: 5
    Last Post: 06-03-2010, 05:39 PM
  5. NullPointerException
    By JohnST in forum New To Java
    Replies: 13
    Last Post: 01-06-2010, 12:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •