Sagewire Logo

Class instatiation via new is null?

5 Message(s) by 3 Author(s) originally posted in java language


From: Jim Michaels Date:   Monday, June 11, 2007
class Vertex4D {
public double x, y, z, w;
Vertex4D() {
x = 0; y = 0; z = 0; w = 0;
}
Vertex4D(double newX, double newY, double newZ, double newW) {
x = newX; y = newY; z = newZ; w = newW;
}
public double getX() {return x;}
public double getY() {return y;}
public double getZ() {return z;}
public double getW() {return w;}
public void setX(double newX) {x = newX;}
public void setY(double newY) {y = newY;}
public void setZ(double newZ) {z = newZ;}
public void setW(double newW) {w = newW;}
}

class text {
Vertex4D[] v;
public boolean done, rotate = false, printMode, objectLoaded=false;
Thread th;

text() {
System.out.println("text constructor");
v = new Vertex4D[20];
if (v == null ) {System.out.println("v=null!"); return;}
v[0].setX(0);
}

public static void main(String args[]) {
System.out.println("main");
//complains about static context of v if I put the above code
in here & get rid of line below.
new text();
}
}
C:\prj\hyprcube3\src>JAVA text
main
text constructor
Exception in thread "main" JAVA.lang.NullPointerException
at text.<init>(text.JAVA:41)
at text.main(text.JAVA:47)

Exception at the v[0].setX(0); line basically. this should not be happening.
I cannot see what I'm doing wrong here. I do not want to set the size of
the array until I know how big it should be.
This should work even if the Vertex4D class doesn't have a constructor.
the class is instantiated the same way I see examples of String arrays
being instantiated. (http://JAVA.about.com/od/beginningJAVA/l/aa_array.htm)

------------------------------------
Jim Michaels
for email, edit the address


From: Jack Marsh Date:   Tuesday, June 12, 2007
wrote in message:
[snip]
class text {
Vertex4D[] v;
public boolean done, rotate = false, printMode, objectLoaded=false;
Thread th;
text() {
System.out.println("text constructor");


here you allocate pointer s to reference 20 Vertex4D objects. Note that
these pointers are all null at this point.
v = new Vertex4D[20];



here you test that these pointers were allocated
if (v == null) {System.out.println("v=null!"); return;}



here you try to use the first pointer without creating a Vertex4D
object. NullPointerException results...
try adding
v[0] = new Vertex4D();
v[0].setX(0);
}


[snip]
Your contention that you've seen code like this used for Strings is
quite wrong. Find that code and look again.

JAVA isn't a particularly easy language to learn by example, but do not
give up, you are making good progress.


From: Jim Michaels Date:   Tuesday, June 12, 2007
wrote in message:
wrote in message:
[snip]
class text {
Vertex4D[] v;
public boolean done, rotate = false, printMode, objectLoaded=false;
Thread th;

text() {
System.out.println("text constructor");
here you allocate pointers to reference 20 Vertex4D objects. Note that
these pointers are all null at this point.
v = new Vertex4D[20];
here you test that these pointers were allocated
if (v == null) {System.out.println("v=null!"); return;}
here you try to use the first pointer without creating a Vertex4D
object. NullPointerException results...
try adding
v[0] = new Vertex4D();
v[0].setX(0);
}
[snip]
Your contention that you've seen code like this used for Strings is
quite wrong. Find that code and look again.
JAVA isn't a particularly easy language to learn by example, but do not
give up, you are making good progress.


for (int index =0; index < v.length; index++) {
v[0] = new Vertex4D();
}

this caused it to work without NPE's. and now I think I understand how
JAVA arrays work. something no example or tutorial has yet showed me.
thanks.
There ought to be a thick book called Real-world JAVA.
--

------------------------------------
Jim Michaels
for email, edit the address

RAM Disk is *not* an installation method.


From: Jack Marsh Date:   Tuesday, June 12, 2007
wrote in message:
wrote in message:
wrote in message:
[snip]
for (int index=0; index < v.length; index++) {
v[0] = new Vertex4D();


^--- I'm sure you meant to put v[index] here.
}
this caused it to work without NPE's. and now I think I understand how
JAVA arrays work. something no example or tutorial has yet showed me.
thanks.
There ought to be a thick book called Real-world JAVA.


There are plenty of thick JAVA books.


From: Lew Date:   Tuesday, June 12, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
[snip]
for (int index=0; index < v.length; index++) {
v[0] = new Vertex4D();
^--- I'm sure you meant to put v[index] here.
}

this caused it to work without NPE's. and now I think I understand
how JAVA arrays work. something no example or tutorial has yet showed
me. thanks.
There ought to be a thick book called Real-world JAVA.
There are plenty of thick JAVA books.



When you create a new array, you don't create n new objects, you create n new
references, all null. You've to set an individual array element to a
non-null value to avoid the NPE.

--
Lew



Next Message: class or interface expected



Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional