Solved

NullPointException error don't see it

Posted on 2004-04-02
22
199 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 90
Non-recursive backtracking, using a stack 1 102
micro services spring boot application error 3 143
eclipse java  build path 6 44
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

910 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

16 Experts available now in Live!

Get 1:1 Help Now