0

i faced a problem with null , i implement the same code before and it work well but after i add other codes its not work ..

this is part of error :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at manager.flyer$13.mouseClicked(flyer.java:751)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)

and my code part which show that error :

JButton btnNewButton_4 = new JButton("Add All");
           btnNewButton_4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            int columnContainingPlanets =1 ;
            for (int index =0 ; index < tableModel.getRowCount(); index ++){
            if (tableModel.getValueAt(index, columnContainingPlanets).equals("")) {
            JOptionPane.showMessageDialog(panel_4, "Please Enter Discount Amount For All Items");

            return; }}

i try to replace columnContainingPlanets=1 to columnContainingPlanets=0 but also there is error : java.lang.NullPointerException

i will happy if any one know the solution because that error appeared suddenly, and it was work well ..

gtrfexws
  • 53
  • 1
  • 8
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Apr 20 '17 at 10:45
  • Look at line 751 in flyer.java - something on that line is null. – greg-449 Apr 20 '17 at 10:45
  • @greg-449 yah thats true , the line is `if(tableModel.getValueAt(index,columnContainingPlanets).equals(""))` So, i need to show message dialog when its null . – gtrfexws Apr 20 '17 at 11:04
  • @andrew-thompso i was see it , but unfortunately its not help me .. – gtrfexws Apr 20 '17 at 13:12
  • I was hoping the first link at least would help you to realise that the line numbers in the stack trace are significant, but useless unless there is a [mcve] that allows others to see the exact line causing the problem. OTOH if you are unable to get any further (in asking a good, to the point question with all necessary information provided to solve it) from a combination of those links, you should seriously ask yourself if programming is your thing. More to the point, SO is a Q&A platform, not a help-desk for every last programming problem that might occur. – Andrew Thompson Apr 20 '17 at 16:20

1 Answers1

0

We can't debug a NullPointerException for you. You are the only person that has access to your code and the data in your application. So you are the only person that can solve the problem.

A few pointers to help with debugging:

  1. Why are you creating a variable (columnContainingPlanets) to use as an index in the getValueAt(...) method. It won't cause a problem but it seems a bit unnecessary.

  2. Don't code multiple statements in the same line of code. It makes it harder to determine which variable is null. In your case I can see that "tableModel" could be null, or the value returned from the TableModel could be null. Until you know which value is null you can't solve the problem.

  3. You can't test for the String "" in the if statement. The getValueAt() method returns an Object. An Object is not a String.

So you might restructure you code something like this:

for (int index =0 ; index < tableModel.getRowCount(); index ++)
{
    Object value = tableModel.getValueAt(index, 1);

    if (value.toString().equals("")) 
    {
        JOptionPane.showMessageDialog(panel_4, "Please Enter Discount Amount For All Items");

        return; 
    }
}

Now the line number given in the NullPointerException will tell you exactly which variable is null. Either:

  1. the "tableModel" is null (in which cause you have a problem with how you reference the table model, or

  2. the "value" is null (in which case you have a problem with the data contained in your table model.

Finally, Don't use a MouseListener on a JButton. Instead you should be adding an ActionListener to the JButton. Read the section from the Swing tutorial on How to Use Buttons. Download the demo code and play with the examples.

camickr
  • 321,443
  • 19
  • 166
  • 288