Solved

NullPointException error don't see it

Posted on 2004-04-02
22
201 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle SQL syntax check  without executing 6 50
type mismatch (Object[] to double[] 4 24
eclipse buid path vs tomcat lib path 10 22
Java: anonymous class 4 18
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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