473,503 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a class from a different file

6 New Member
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
Dec 7 '09 #1
5 2502
chaarmann
785 Recognized Expert Contributor
Call the second file "Square.java" which contains a "Square" class with the method "draw()".
Now you can call it from your first file with:
squareObject = new Square();
squareObject.draw();
Dec 7 '09 #2
Narutodono
6 New Member
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.
Dec 7 '09 #3
chaarmann
785 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1. Tile tile = new Tile();
  2. tile.draw();
  3.  
remark:
Of course you could also call "Tile.draw()", but then you must have declared the method draw() as "static" before ....
Dec 7 '09 #4
Narutodono
6 New Member
makes sense now thankyou :) but i have encountered a error.

nullpointerexception, it points to the graphics g. me and my friend have the same is there a general reason behind this error?
Dec 7 '09 #5
chaarmann
785 Recognized Expert Contributor
nullpointerexception 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.
Dec 8 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4445
by: Marc Shapiro | last post by:
I am relatively new to python (I have used it on and off for a few small projects over the last few years) so I imagine that what I am trying to do has already been done, but practical experience,...
4
5119
by: Martin Maney | last post by:
I've been to the cookbook (both forms, since they're sometimes usefully different), but that gave rise to more questions than answers. Heck, let me pull some of them up while I'm at it. ...
1
2301
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between...
19
4221
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
3394
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
12
3000
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
4
1213
by: wisaunders | last post by:
I have several different classes that contain the same four properties. I tell my application property of which class to call in the configuration file. Here are the values from my config file...
4
2776
by: Dan | last post by:
Hi All, I've got a problem with my C++ application that calls a Java class that I've built with GCJ, I can't run it because I get errors: multiple definition of `atexit' first defined here...
6
3596
by: ranin02 | last post by:
Hi, I have a COM method that is called from VBA. This COM method in turn calls CoCreateInstance on a class that is a .NET class with a COM wrapper around it. This all works fine. However,...
7
2663
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.