Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

NullPointException error don't see it

Posted on 2004-04-02
22
Medium Priority
?
214 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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

876 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