Solved

NullPointException error don't see it

Posted on 2004-04-02
22
196 Views
Last Modified: 2010-03-31
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  //starts class
        JPanel jPanel;
        ArrayList myArrayList = new ArrayList();

    // Constructor
    public MyClass4() { //starts constructor
       
        setLayout(new GridLayout(0,3)); // Setting MY layout...
         looprow:
    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {
    int n=(20*col+row+1);
    if (n>50) break looprow;
    JCheckBox check = new JCheckBox("" + n);
    myArrayList.add(check);
    add(check); // Adding it to ME

        jPanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                jPanel.setBackground(Color.blue);
                }
                public void mouseClicked(MouseEvent e) {
                    jPanel.setBackground(Color.red);
                    }
                    public void mouseExited(MouseEvent e) {
                        jPanel.setBackground(Color.white);
                        }
                        });
}
}
    }//ends constructor
}// ends class
0
Comment
Question by:Drop_of_Rain
  • 9
  • 9
  • 4
22 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
jPanel never gets created.

change to:

JPanel jPanel = new JPanel();
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I INCREASED THE POINTS BECAUSE IT JUST GOT HARDER

I TRIED THAT TO CHANGE THE COLOR OF THE PANEL WITH THE CHECKBOXS IN IT IT DIDN'T WORK, HELP
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
Comment Utility
you never add the panel but u don't really need it.

        jPanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                setBackground(Color.blue);
                }
                public void mouseClicked(MouseEvent e) {
                    setBackground(Color.red);
                    }
                    public void mouseExited(MouseEvent e) {
                        setBackground(Color.white);
                        }
                        });
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
This should solve your problem :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  //starts class

    JPanel jPanel;
    ArrayList myArrayList = new ArrayList();

    public MyClass4() { //starts constructor
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3)); // Setting MY layout...

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {
    int n=(20*col+row+1);
    if (n>50) break looprow;
    JCheckBox check = new JCheckBox("" + n);
    myArrayList.add(check);
    add(check); // Adding it to ME

        jPanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                jPanel.setBackground(Color.blue);
                }
                public void mouseClicked(MouseEvent e) {
                    jPanel.setBackground(Color.red);
                    }
                    public void mouseExited(MouseEvent e) {
                        jPanel.setBackground(Color.white);
                        }
                        });
      }
      }
    }//ends constructor
}// ends class
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
and:

    addMouseListener(new MouseAdapter() {
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I guess I can't change the color in the checkbox gui itself.
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Is there a way of having the text color change after the checkbox is checked?
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I just raised the points again
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Yes you can if its a check box then its like :

  JCheckBox check = new JCheckBox();

  check.addItemListener(new ItemListener() {

  public void itemStateChanged(ItemEvent e)
  {
  // To change the color of the checkbox . . .
  check.setBackground(Color.blue);
  }
  });

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
OR :

  JCheckBox check = new JCheckBox();

  check.addItemListener(new ItemListener() {

  public void itemStateChanged(ItemEvent e)
  {
  if (e.getStateChange() == ItemEvent.SELECTED) {
  // To change the color of the checkbox . . .
  check.setBackground(Color.blue);  
  }
  else if(e.getStateChange() == ItemEvent.DESELECTED) {
  }
  // To change the color of the checkbox . . .
  check.setBackground(Color.red);
  }
  });

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Sorry it should be :

  JCheckBox check = new JCheckBox();

  check.addItemListener(new ItemListener() {

  public void itemStateChanged(ItemEvent e)
  {
  if (e.getStateChange() == ItemEvent.SELECTED) {
  // To change the color of the checkbox . . .
  check.setBackground(Color.blue);  
  }
  else if(e.getStateChange() == ItemEvent.DESELECTED) {
  // To change the color of the checkbox . . .
  check.setBackground(Color.red);
  }
  }
  });
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Drop_of_Rain
Comment Utility
Can the panel itself that the checkboxs are on in Myclass4

  check.setBackground(Color.blue);   local variable is called in inner class need to be declared final


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  //starts class
    final JCheckBox check;
    JPanel jPanel;
    ArrayList myArrayList = new ArrayList();

    public MyClass4() { //starts constructor
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3)); // Setting MY layout...

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {
    int n=(20*col+row+1);
    if (n>50) break looprow;
    JCheckBox check = new JCheckBox("" + n);
    myArrayList.add(check);
    add(check); // Adding it to ME
       
        check.addItemListener(new ItemListener() {

         public void itemStateChanged(ItemEvent e)
         {
             if (e.getStateChange() == ItemEvent.SELECTED) {
                 // To change the color of the checkbox . . .
                 check.setBackground(Color.blue);  
                 }
                 else if(e.getStateChange() == ItemEvent.DESELECTED) {
                     }
                     // To change the color of the checkbox . . .
                     check.setBackground(Color.red);
                     }
                     });
       
        jPanel.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                setBackground(Color.blue);
                }
                public void mouseClicked(MouseEvent e) {
                    setBackground(Color.red);
                    }
                    public void mouseExited(MouseEvent e) {
                        setBackground(Color.white);
                        }
                        });


 

     }
     }
    }//ends constructor
}// ends class
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
This should solve your problem :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  

    ArrayList myArrayList = new ArrayList();

    JPanel jPanel;
    JCheckbox check;


    public MyClass4() {
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3));

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {
    int n=(20*col+row+1);
    if (n>50) break looprow;

    check = new JCheckBox("" + n);

    myArrayList.add(check);
    add(check);
       
    check.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getStateChange() == ItemEvent.SELECTED) {
    // To change the color of the checkbox . . .
    check.setBackground(Color.blue);  
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED) {
    }
    // To change the color of the checkbox . . .
    check.setBackground(Color.red);
    }
    });
       
    jPanel.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
    jPanel.setBackground(Color.blue);
    }
    public void mouseClicked(MouseEvent e) {
    jPanel.setBackground(Color.red);
    }
    public void mouseExited(MouseEvent e) {
    jPanel.setBackground(Color.white);
    }
    });
 
    }
    }
    }
}
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
ERROR SAYS IT CAN'T RESOLVE CLASS CHECKBOX


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  

    ArrayList myArrayList = new ArrayList();

    JPanel jPanel;
    JCheckbox check;


    public MyClass4() {
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3));

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {
    int n=(20*col+row+1);
    if (n>50) break looprow;

    check = new JCheckBox("" + n);

    myArrayList.add(check);
    add(check);
       
    check.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getStateChange() == ItemEvent.SELECTED) {
    // To change the color of the checkbox . . .
    check.setBackground(Color.blue);  
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED) {
    }
    // To change the color of the checkbox . . .
    check.setBackground(Color.red);
    }
    });
       
    jPanel.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {
    jPanel.setBackground(Color.blue);
    }
    public void mouseClicked(MouseEvent e) {
    jPanel.setBackground(Color.red);
    }
    public void mouseExited(MouseEvent e) {
    jPanel.setBackground(Color.white);
    }
    });
 
    }
    }
    }
}
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
I've rearranged your codes for it to look good :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  

  private JPanel jPanel;
  private JCheckbox check;

  ArrayList myArrayList = new ArrayList();

    public MyClass4() {
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3));

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {

    int n=(20*col+row+1);
    if (n>50) break looprow;

    check = new JCheckBox("" + n);

    myArrayList.add(check);
    add(check);
       
    check.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getStateChange() == ItemEvent.SELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.blue);  
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.red);
    }
    }
    });
       
    jPanel.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.blue);
    }
    public void mouseClicked(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.red);
    }
    public void mouseExited(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.white);
    }
    });
    }
    }
  }
}
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
SAME ERROR MESSAGE
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
ERROR SAYS IT CAN'T RESOLVE SYMBOL CLASS JCHECKBOX
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
I'm Sorry this :
private JCheckbox check;

Should be :
private JCheckBox check;

This is final answer :


import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JFrame {  

 ArrayList myArrayList = new ArrayList();

 private JPanel jPanel;
 private JCheckBox check;

    public MyClass4() {
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3));

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {

    int n=(20*col+row+1);
    if (n>50) break looprow;

    check = new JCheckBox("" + n);

    myArrayList.add(check);
    add(check);
       
    check.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getStateChange() == ItemEvent.SELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.blue);  
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.red);
    }
    }
    });
       
    jPanel.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.blue);
    }
    public void mouseClicked(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.red);
    }
    public void mouseExited(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.white);
    }
    });
 
    }
    }
    setSize(300,300);
    setVisible(true);
    }
    public static void main(String args[]) {
    MyClass4 x = new MyClass4();
    x.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

Hope this solves the problem . . .
Friend : Javatm

0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
Need to go home now, you can close this if you think that your
question was answered.

Thank you very much for your time.
Friend : Javatm
0
 
LVL 15

Accepted Solution

by:
Javatm earned 150 total points
Comment Utility
Sorry again the prevous was for a JFrame this is the one for you
since you are using a JPanel :

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MyClass4 extends JPanel {  

  private JPanel jPanel;
  private JCheckBox check;

  ArrayList myArrayList = new ArrayList();

    public MyClass4() {
       
    jPanel = new JPanel();
    setLayout(new GridLayout(0,3));

    add(jPanel);
   
    looprow:

    for (int row=0; row<20; row++) {
    for(int col=0; col<3; col++) {

    int n=(20*col+row+1);
    if (n>50) break looprow;

    check = new JCheckBox("" + n);

    myArrayList.add(check);
    add(check);
       
    check.addItemListener(new ItemListener() {

    public void itemStateChanged(ItemEvent e)
    {
    if (e.getStateChange() == ItemEvent.SELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.blue);  
    }
    else if(e.getStateChange() == ItemEvent.DESELECTED) {

    // To change the color of the checkbox . . .
    check.setBackground(Color.red);
    }
    }
    });
       
    jPanel.addMouseListener(new MouseAdapter() {
    public void mouseEntered(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.blue);
    }
    public void mouseClicked(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.red);
    }
    public void mouseExited(MouseEvent e) {

    // To change the color of the panel . . .
    jPanel.setBackground(Color.white);
    }
    });
    }
    }
  }
}
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Thanks for all the help.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now