Solved

NullPointException error don't see it

Posted on 2004-04-02
7
201 Views
Last Modified: 2010-03-31
It happen here:  add(jLabel1);  jLabel1.setText("Defensive Moves"); setBackground(Color.white);




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

public class MyClass4 extends JPanel {
  JLabel jLabel1;  
 
  ArrayList myArrayList = new ArrayList();

// Constructor
public MyClass4() {

    setLayout(new GridLayout(0,3)); // Setting MY layout...

add(jLabel1);  jLabel1.setText("Defensive Moves"); setBackground(Color.white);

    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
}
}
}
}
0
Comment
Question by:Drop_of_Rain
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 10746541
jLabel1 is null.

Change declaration to:
  JLabel jLabel1 = new JLabel();  
0
 
LVL 92

Expert Comment

by:objects
ID: 10746586
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10746589
Friend;

You need to also call the jLabel after you had declared it like :

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

public class MyClass4 extends JPanel {

  JLabel jLabel1;  
  ArrayList myArrayList = new ArrayList();

public MyClass4() {

     // Call it like this . . .
    jLabel = JLabel();

    setLayout(new GridLayout(0,3)); // Setting MY layout...
 
    add(jLabel1);  
    jLabel1.setText("Defensive Moves");
    setBackground(Color.white);

    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);
    }
    }
}
}

Hope that helps . . .
Javatm
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 15

Expert Comment

by:Javatm
ID: 10746614
Your 2 fast you think you havent seen my solution.
0
 

Author Comment

by:Drop_of_Rain
ID: 10746654
Error can't resolve method jLabel1()



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

public class MyClass4 extends JPanel {

  JLabel jLabel1;  
  ArrayList myArrayList = new ArrayList();

public MyClass4() {

     // Call it like this . . .
    jLabel = JLabel();

    setLayout(new GridLayout(0,3)); // Setting MY layout...
 
    add(jLabel1);  
    jLabel1.setText("Defensive Moves");
    setBackground(Color.white);

    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);
    }
    }
}
}
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10747819
jLabel = JLabel();

should be

jLabel = new JLabel();   //to initialise a new JLabel you use the new operator.
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10747824
it should be jLabel1 = new jLabel() actually, you don't have any object called jlabel
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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 …

708 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

21 Experts available now in Live!

Get 1:1 Help Now