Solved

NullPointException error don't see it

Posted on 2004-04-02
22
203 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
ID: 10746695
jPanel never gets created.

change to:

JPanel jPanel = new JPanel();
0
 

Author Comment

by:Drop_of_Rain
ID: 10746698
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
ID: 10746713
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:Javatm
ID: 10746714
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
ID: 10746715
and:

    addMouseListener(new MouseAdapter() {
0
 

Author Comment

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

Author Comment

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

Author Comment

by:Drop_of_Rain
ID: 10746735
I just raised the points again
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10746736
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
ID: 10746743
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
ID: 10746748
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
 

Author Comment

by:Drop_of_Rain
ID: 10746764
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
ID: 10746773
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
ID: 10746789
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
ID: 10746791
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
ID: 10746796
SAME ERROR MESSAGE
0
 

Author Comment

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

Expert Comment

by:Javatm
ID: 10746803
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
ID: 10746854
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
ID: 10746863
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
ID: 10751289
Thanks for all the help.
0
 
LVL 92

Expert Comment

by:objects
ID: 10753567
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

820 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