Im having to create a java program for a assignment and we have to use 2 files to create the program, basicly i need to create a program that will create a GUI i will create the frame and panel in the main first file, then i need to draw a 5x5 squares vbut this has to be done in the second file and i dont know how i could call it in the first file. any help is appreciated. Thanks
Calling a class from a different file
Collapse
X
-
Tags: None
-
ive done as you said but instead used the name Tile.java
tile = new Tile();
Tile.draw();
but this produces a error the underlined areas are the two errors i keep trying to get around this i cannt see what is causing this. the error says cannot find symbol and they are both associated with this file so it isnt the Tile.java file i believe.Comment
-
you are calling class dot method.
But in my example I called object dot method.
Giving the same name for class and object, only with uppercase and lowercase letter as a difference, is a confusing programming style and should be avoided, because it leads to the error you experienced.
So in your case, correct is:
remark:Code:Tile tile = new Tile(); tile.draw();
Of course you could also call "Tile.draw( )", but then you must have declared the method draw() as "static" before ....Comment
-
makes sense now thankyou :) but i have encountered a error.
nullpointerexce ption, it points to the graphics g. me and my friend have the same is there a general reason behind this error?Comment
-
nullpointerexce ption occurs if the object isn't there.
The variable that holds the reference to the object is null instead of pointing to the object.
It could have many reasons. To figure out the exact one, you need to provide the source code and the error message here.
Generally speaking, just check every variable if it is null before passing it to other methods or after you received it as a return value from a method. That means check if you are passing garbage. Garbage-in-garbage-out. If you pass null, either you get a crash or get null back.Comment
Comment