Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi There, I've got some problems

Status
Not open for further replies.

JProg

Programmer
Apr 4, 2002
88
0
0
JP
Hi There,

I've got some problems with constructor methods in my program, of course the constructors look fine to me, but then faulty code usually looks good to any programmer after enough hours! Here is some of my code, firstly the following snippet shows the objects being instantiated.


public static ButtonData BtData1;
public static ButtonData BtData2;
public static ButtonData BtData3;
public static ButtonData BtData4;
public static ButtonData BtData5;

public static void main(String[] args)
{
try{
inStream = new FileInputStream("ObjStore"); objStreamIn = new ObjectInputStream(inStream);
if(objStreamIn.available() == 0)
{
objStreamIn.close();
inStream.close();
BtData1 = new ButtonData("Link1",null);
BtData2 = new ButtonData("Link2",null);
BtData3 = new ButtonData("Link3",null);
BtData4 = new ButtonData("Link4","Zero");
BtData5 = new ButtonData("Link5","Zero");
}
else
{
......
......
......
}
}catch(IOException e){}
GUI xyz = new GUI();
}
}


Alot of the above code is related to serialization -- mainly checking to see whether or not objects already exist on file, and if they do not, instantiating them. Notice that the above constructors have different parameters or arguments (I can't remember which word describes it best!) -- it doesn't seem to matter whether I use two strings such as ("Link4","Zero") or ("Link1",null) -- both create errors. The class that is instantiated looks like this:


import java.io.*;

public class ButtonData implements Serializable{

String Name;
String Path;
ButtonData(String Name,String Path)
{
this.Name = Name;
this.Path = Path;
}

protected String getName()
{
return Name;
}
protected String getPath()
{
return Path;
}
protected void setName()
{
Name = Begin.LinkName.getText();
}
protected void setPath()
{
Path = Begin.ExecT.getText();
}
}


As far as I can tell the constructor above follows all the relevant syntax rules, as does the first snippet of code that I provided -- where the objects are instantiated. I will probably feel like a real meat head if I have done something dump and obvious, however I have double checked the code and it looks fine to me. When trying to compile the code I get the following error messages (using JBuilder7) -- these error messages highlight the relevant sections of the first code snippet I provided:


"Begin.java": Error #: 300 : constructor ButtonData(java.lang.String, null) not found in class quicklauncher2.ButtonData at line 61, column 23"Begin.java": Error #: 300 : constructor ButtonData(java.lang.String, null) not found in class quicklauncher2.ButtonData at line 62, column 23"Begin.java": Error #: 300 : constructor ButtonData(java.lang.String, null) not found in class quicklauncher2.ButtonData at line 63, column 23"Begin.java": Error #: 300 : constructor ButtonData(java.lang.String, java.lang.String) not found in class quicklauncher2.ButtonData at line 64, column 23"Begin.java": Error #: 300 : constructor ButtonData(java.lang.String, java.lang.String) not found in class quicklauncher2.ButtonData at line 65, column 23


If anybody can shed some light on what is going on here it will be greatly appreciated.

Thanks
 
Since you didn't give the full code, I can't say for certain, but did you make sure that your class imports the ButtonData class properly?
Kris Simonis
Topdesk Information Systems,
Application Server Development

"You haven't seen the last of Meeaaaarrrrghh!!!"
- Several bad guys in several bad movies

 
Can't pass in null for a String. You can use "" and check for length() > 0. [morning] HavaTheJut
 
Sorry about that previous post... I wan't thinking. [morning] HavaTheJut
 
Sorry to correct you Hava, but you can pass null as a string. Kris Simonis
Topdesk Information Systems,
Application Server Development

"You haven't seen the last of Meeaaaarrrrghh!!!"
- Several bad guys in several bad movies

 
it probably would, otherwise it wouldnt of given him those error, more like java.lang.NoClassDefFound exceptions.


the constructor needs to be public.


public ButtonData(blarg)
{
}

 
btw by "it probably would" i meant "he probably has included the class correctly".

sorry :0)

 
well default will work if it's in the same package anyways (another words same directory). I don't understand the protected accesser methods on the default fields(protected being that only this class and all classes that extend this class can access). Better to have public methods and private data. Also try importing the file into the test code like import ButtonData, or if in a sub directory, import mydir.ButtonData; it looks to me as if it is accessing the wrong class.
 
Where's the unpost button when you need it? :)

Anyway, this error seems similar to the one you got. (I can't tell for sure, since you used borland and this is from javac)
Code:
java.lang.NoSuchMethodError: Blop.<init>(Ljava/lang/String;)at ConstTest.main(ConstTest.java:5)

In order to get this error I had two classes: Blop with a single constructer Blop(String s) (default, not public, mind you - the constructor need not be public for same-package access) and ConstTest that constructed one Blop in the main method. I compiled both files and they ran fine. Then I changed the Blop constructor to take in two strings and recompiled only that file. At runtime, this error was displayed.

Assuming that this is the same error you got, I can think of two possible causes for your error:
1. For some reason, some of your files did not get compiled again after you changed the constructor.
2. You recompiled everything correctly, but the old class files are still being accessed (i.e. if you moved the new ones) You might be looking at the wrong package/class directory (classpath? import statement?)

Hope this helps. [morning] HavaTheJut
 
Thanks to everybody who constributed pointers:

JavaDude32: I followed your suggestions concerning changing the variables to private and making accessor and mutator methods public.

HavaTheJut (Obviously a Star Wars fan!); I checked my class paths and all of my five classes refer to the same package. Also it doesn't seem to matter whether or not I use a String such as &quot;Link1&quot; or null in the constructor in both instances I get an unsatisfactory result.

Again, thanks heaps for your time and I will keep an eye on this thread...hoping that an answer will evolve.
 
Thanks to everybody who constributed pointers:

JavaDude32: I followed your suggestions concerning changing the variables to private and making accessor and mutator methods public.

HavaTheJut (Obviously a Star Wars fan!); I checked my class paths and all of my five classes refer to the same package. Also it doesn't seem to matter whether or not I use a String such as &quot;Link1&quot; or null in the constructor in both instances I get an unsatisfactory result.

Again, thanks heaps for your time and I will keep an eye on this thread...hoping that an answer will evolve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top