Got Problem With Class Array

import javax.swing.*;

public class test{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public test(){

bursts = 0;

}

public test(int len) {

length = len;

burst = new Integer[length];

}

public void setLength(int i){

length = i;

burst = new Integer[length];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

test[] m = new test[5];

test a = new test();

test b = new test();

a.setBurst( 5, 0);

b.setBurst(10, 1);

m[0].setBurst(6, 0);

m[1].setBurst(16, 2);

System.out.print(a.getBurst(0)+" "+b.getBurst(1)+" "+m[0].getBurst(0)+" "+m[1].getBurst(1));

for(int i=0; i<5; i++){

m.setBurst(Integer.parseInt(JOptionPane.showInputDialog("What Is The Burst Time of Process"+(i+1)+"?")),i);

}

}

}

thats my code... my problem is why can't i make the test class as an array?! it always keeps on having a null pointer exception... can sum1 help me pls...

[1270 byte] By [Mark_Ramosa] at [2007-10-3 3:17:35]
# 1
Please post your code properly, using [code][/code] tags, and indenting it properly. Also post the stack trace you're getting. The stack trace will tell you what line is causing the null pointer exception.
paulcwa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 2

import javax.swing.*;

public class test{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public test(){

bursts = 0;

}

public test(int len) {

length = len;

burst = new Integer[length];

}

public void setLength(int i){

length = i;

burst = new Integer[length];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

test[] m = new test[5];

test a = new test();

test b = new test();

a.setBurst( 5, 0);

b.setBurst(10, 1);

m[0].setBurst(6, 0);

m[1].setBurst(16, 2);

System.out.print(a.getBurst(0)+" "+b.getBurst(1)+" "+m[0].getBurst(0)+" "+m[1].getBurst(1));

for(int i=0; i<5; i++){

m.setBurst(Integer.parseInt(JOptionPane.showInputDialog("What Is The Burst Time of Process"+(i+1)+"?")),i);

}

}

}

there... i really know wat line has the error... but i wanted help to make this code correct.. coz i cant get it anymore... pls help..

Mark_Ramosa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 3
Cornfused ... this code does not compile - so how can you be getting a run-time excep?
abillconsla at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 4
Never mind ... I forgot about auto-boxing on 1.5x; I am still on 1.4x.
abillconsla at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 5

import javax.swing.*;

public class test{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public test(){

bursts = 0;

}

public test(int len) {

length = len;

burst = new Integer[length];

}

public void setLength(int i){

length = i;

burst = new Integer[length];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

test[] m = new test[5];

test a = new test();

test b = new test();

a.setBurst( 5, 0);

b.setBurst(10, 1);

m[0].setBurst(6, 0);

m[1].setBurst(16, 2);

System.out.print(a.getBurst(0)+" "+b.getBurst(1)+" "+m[0].getBurst(0)+" "+m[1].getBurst(1));

}

}

theres the new code.. and here's the error....

Exception in thread "main" java.lang.NullPointerException

at test.main(test.java:36)

pls help me correct my code.. teach me how to make this Class An Array.. Just like classes like String[], JButton[], Class[]... thats my problem.. is there sumthing missing in my code? sumthing wrong obviously... pls help me guys :D

Mark_Ramosa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 6

Changed a few line so I could compile on 1.4x. Then changed the pblm code:

import javax.swing.*;

public class TestArrayClass{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public TestArrayClass(){

bursts = 0;

}

public TestArrayClass(int len) {

length = new Integer(len);

burst = new Integer[length.intValue()];

}

public void setLength(int i){

length = new Integer(i);

burst = new Integer[length.intValue()];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

TestArrayClass[] m = new TestArrayClass[5];

TestArrayClass a = new TestArrayClass();

TestArrayClass b = new TestArrayClass();

a.setBurst( 5, 0);

b.setBurst(10, 1);

m[0] = a;

m[1] = b;

m[0].setBurst(6, 0);

m[1].setBurst(16, 2);

System.out.print(a.getBurst(0)+" "+b.getBurst(1)+" "+m[0].getBurst(0)+" "+m[1].getBurst(1));

for(int i=0; i<5 && m[i] != null; i++){

String result = JOptionPane.showInputDialog("What Is The Burst Time of Process"+(i+1)+"?");

m[i].setBurst(Integer.parseInt(result),i);

System.out.print("\nm["+i+"].getBurst(0): "+m[i].getBurst(1));

}

}

}

abillconsla at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 7

import javax.swing.*;

public class TestArrayClass{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public TestArrayClass(){

bursts = 0;

}

public TestArrayClass(int len) {

length = new Integer(len);

burst = new Integer[length.intValue()];

}

public void setLength(int i){

length = new Integer(i);

burst = new Integer[length.intValue()];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

TestArrayClass[] m = new TestArrayClass[5];

//why should this be needed to create an array class?

for(int i=0; i<5; i++){

m[i] = new TestArrayClass();

}

for(int i=0; i<5; i++){

String result = JOptionPane.showInputDialog("What Is The Burst Time of Process"+(i+1)+"?");

m[i].setBurst(Integer.parseInt(result),i);

System.out.print("\nm["+i+"].getBurst(0): "+m[i].getBurst(1));

}

}

}

alright i got ur code but there's a problem.. why do i nid to do that line after comment? isnt new keyword means instantiation? for example... String[] a = new String[5];

there's no code that instantiates each... it already instantiates itself... is there sumthing i should add in the constructors?! pls help again... :D

Mark_Ramosa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 8

TestArrayClass[] m = new TestArrayClass[5]; // Arrays are Object in Java; hense the "new"

TestArrayClass a = new TestArrayClass(); // New TestArrayClass Object

TestArrayClass b = new TestArrayClass(); // ditto

// a.setBurst( 5, 0); // 4get this for now

// b.setBurst(10, 1); // 4get this for now

m[0] = a; // Even though m as an array is an Object in and of itself,

// it will still need to be populated with bonafide TestArrayClass Objects

m[1] = b; // ditto

m[0].setBurst(6, 0);

m[1].setBurst(16, 2);

System.out.print(m[0].getBurst(0)+" "+m[1].getBurst(1));

for(int i=0; i<5 && m[i] != null; i++) { //Because you have made your array length '5',

// but have only populated elements 0 and 1, you need some,

// way of preventing the loop from trying to process after that point.

//...

}

abillconsla at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 9

import javax.swing.*;

public class TestArrayClass{

private Integer length;

private static Integer[] burst;

private static Integer[] array;

private static Integer content;

private int bursts;

public TestArrayClass(){

bursts = 0;

}

public TestArrayClass(int len) {

length = new Integer(len);

burst = new Integer[length.intValue()];

}

public void setLength(int i){

length = new Integer(i);

burst = new Integer[length.intValue()];

}

public void setBurst(int args, int i){

bursts = args;

}

public int getBurst(int i){

return bursts;

}

public static void main(String args[]){

TestArrayClass[] m = new TestArrayClass[5];

//why should this be needed to create an array class?

for(int i=0; i<5; i++){

m[i] = new TestArrayClass();

}

//end of instantiation

for(int i=0; i<5; i++){

String result = JOptionPane.showInputDialog("What Is The Burst Time of Process"+(i+1)+"?");

m[i].setBurst(Integer.parseInt(result),i);

System.out.print("\nm["+i+"].getBurst(0): "+m[i].getBurst(1));

}

}

}

hey there! just want to ask isn't Class[] object = new Class[length] creating an array of the Class? y doesnt it work for me? y do i still nid to instantiate new TestArrayClass(); for each index of the Array? i thought Class[] object = new Class[length] is already creating and instantiating of arrays... and with the new keyword i thought each object of the array is already instantiated.. just like String[] sample = new String[5]; sample[0] is already null, sample[1] is already null until sample[5] right? y cant it happen in my code? i hope u get it...

Mark_Ramosa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 10

I am going to make a box that can contain 100 apples. When the box is made how many apples does it contain?

Hopefully your answer is 0. I now have to pick the apples and store them in the box.

TestArrayClass[] m = new TestArrayClass[5];

In your case, m is the box that can contain 5 objects of TestArrayClass but it does not automagically create those 5 objects for you. you have to create them and place them in your box yourself.

floundera at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 11
so you mean that i stil nid to do this code? for(int i=0; i<5; i++){m[i] = new TestArrayClass();}is that right?
Mark_Ramosa at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...
# 12

I think "flounder"'s example is a very good one - although I think I would prefer 'basket' in the case of apples ... picky picky ;o)

At any rate, you yourself state:

"String[] sample = new String[5]; sample[0] is already null, sample[1] is already null until sample[5] right?"

... Well that's exactly what you got! ... an array of length '5', with null for each element. Whenever you create an array it will leave each element "defaulting" to what is called for; for int that would be '0', for objects - like String - that would be null.

As to your for loop, the control should idealy be the array.length, not a constant (i.e.: i < m.length)

~Bill

Message was edited by:

abillconsl

abillconsla at 2007-7-14 21:09:11 > top of Java-index,Java Essentials,New To Java...