473,379 Members | 1,190 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Calling a class from a different file

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 2496
chaarmann
785 Expert 512MB
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
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 Expert 512MB
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
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 Expert 512MB
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.