different applications same code

Hi!

I just made a tiny litte programm.

It's a JPanel, with JLabels an one JButton on it. The JLabels are instances of some other class.

The weird thing is, that starting the application, the same way!!! ... run as ..., the count of the Labels differ. Actually I added 4 JLabels and one button:

there are sometimes:

4 labels 1 button

1 label

2 labels

showing up.

WHY?

[428 byte] By [Tille2a] at [2007-11-27 0:48:55]
# 1
Hi, Can you provide some code please ?Jack
jack@square6a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 2

sure! I just thought about this;)!!

(don't bother, I knwo test should have been written with uppercase-t!)sorry

-

public class test extends JPanel {

public Kasten [] ak;

ArrayList<Kasten> a;

public test() {

super();

}

public static void main(String[] args) {

JFrame f = new JFrame("test");

f.setPreferredSize(new Dimension(300,400));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test myTest = new test();

f.setContentPane(myTest);

f.pack();

f.setVisible(true);

myTest.setLayout(new FlowLayout());

Kasten k1 = new Kasten(9999, Color.yellow);

myTest.addItem(k1);

Kasten k2 = new Kasten(2);

myTest.addItem(k2);

JButton b = new JButton("button");

myTest.addItem(b);

Kasten kasten = new Kasten(123, Color.red);

myTest.addItem(kasten);

Kasten kasten1 = new Kasten(1, Color.green);

myTest.addItem(kasten1);

myTest.setVisible(true);

}

public void addItem(JButton b){

b.setText("in addItem");

b.setOpaque(true);

this.add(b);

}

public boolean addItem(Kasten arg0) {

System.out.println(arg0.i); // add JLabel to

arg0.setBorder(BorderFactory.createEtchedBorder());

arg0.setOpaque(true);

this.add(arg0);

return a.add(arg0);

}

-

public class Kasten extends JLabel{

int i;

List<Kasten> a = new ArrayList<Kasten>();

public Kasten(int in){

this.setBackground(Color.blue);

this.setForeground(Color.black);

this.setBorder(BorderFactory.createBevelBorder(5));

this.setBounds(20,100, 20,50);

i = in;

this.setText(""+i);

setOpaque(true);

}

public Kasten(int in, Color color){

this.setBackground(color);

this.setForeground(Color.black);

this.setBorder(BorderFactory.createBevelBorder(5));

this.setBounds(20,100, 50,50);

i = in;

this.setText(""+i);

setOpaque(true);

}

...

Tille2a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 3
Hmm..Can you try enlarging the frame to see whether the components are there?
Icycoola at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Good Idea:)I tried it right away, but: no more components!!!Hope I still will understand this! It's so weird?do you have an idea where else to look for an answer?
Tille2a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 5
try asking in the swing forum
Alandera at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 6
In your class test you never initialise ArrayList<Kasten> a;which causes a NPE. I don't see how your code can ever work!
sabre150a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 7
Well, it works, because, I initialize Kasten-objects in the text-class.Don't it?
Tille2a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...
# 8

> Well, it works, because, I initialize Kasten-objects

> in the text-class.

> Don't it?

I don't know but when I run the following

package me.sabre150.research.scratch;

import java.util.*;

import javax.swing.*;

import java.awt.*;

public class test extends JPanel

{

public Kasten [] ak;

ArrayList<Kasten> a;// = new ArrayList<Kasten>();

public test()

{

super();

}

public static void main(String[] args)

{

JFrame f = new JFrame("test");

f.setPreferredSize(new Dimension(300,400));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test myTest = new test();

f.setContentPane(myTest);

f.pack();

f.setVisible(true);

myTest.setLayout(new FlowLayout());

Kasten k1 = new Kasten(9999, Color.yellow);

myTest.addItem(k1);

Kasten k2 = new Kasten(2);

myTest.addItem(k2);

JButton b = new JButton("button");

myTest.addItem(b);

Kasten kasten = new Kasten(123, Color.red);

myTest.addItem(kasten);

Kasten kasten1 = new Kasten(1, Color.green);

myTest.addItem(kasten1);

myTest.setVisible(true);

}

public void addItem(JButton b)

{

b.setText("in addItem");

b.setOpaque(true);

this.add(b);

}

public boolean addItem(Kasten arg0)

{

System.out.println(arg0.i); // add JLabel to

arg0.setBorder(BorderFactory.createEtchedBorder());

arg0.setOpaque(true);

this.add(arg0);

return a.add(arg0);

}

}

class Kasten extends JLabel

{

int i;

java.util.List<Kasten> a = new ArrayList<Kasten>();

public Kasten(int in)

{

this.setBackground(Color.blue);

this.setForeground(Color.black);

this.setBorder(BorderFactory.createBevelBorder(5));

this.setBounds(20,100, 20,50);

i = in;

this.setText(""+i);

setOpaque(true);

}

public Kasten(int in, Color color)

{

this.setBackground(color);

this.setForeground(Color.black);

this.setBorder(BorderFactory.createBevelBorder(5));

this.setBounds(20,100, 50,50);

i = in;

this.setText(""+i);

setOpaque(true);

}

}

I get as output 9999

Exception in thread "main" java.lang.NullPointerException

at me.sabre150.research.scratch.test.addItem(test.java:85)

at me.sabre150.research.scratch.test.main(test.java:54)

with the screen showing a frame with just one component BUT when I change the lineArrayList<Kasten> a;// = new ArrayList<Kasten>();

toArrayList<Kasten> a = new ArrayList<Kasten>();

I get no exception and I see all the components.

So - who is right?

sabre150a at 2007-7-11 23:18:09 > top of Java-index,Java Essentials,Java Programming...