Question:
Error: Variable might not have been initialized??
Carebear
2007-11-12 21:00:48 UTC
I am working on this project for my class, and this is what I have set up:

public class Kennel
{
private Cat [] aKennel;
private int myCatNumber;
private Cat myCat;
Cat icat;

public Kennel()
{
aKennel = new Cat[10];
}

public void removeCat( String iname )
{
Cat icat;
** if( iname == icat.getName() ) **
{
myCatNumber--;
System.out.println( iname + " has been removed." );
}
else
{
System.out.println( "Sorry, we don't have any cats by that name." );
}
}

And on the ** ** line I get an error that variable icat might not have been initialized.
I have no idea if this will work anyway, but I wanted to try it. I have no idea how to fix this error. Please help?
Five answers:
swmighty
2007-11-12 21:35:05 UTC
You have Cat icat (should be iCat, but that is ok) listed in your instance data. You then have your remove method that also declares a local instance of Cat called iCat. iCat has to be something. I know that iCat is declared as an object from another class called Cat. You already have an instance of Cat in your array of Cat right? So just use that in your remove method:



public void removeCat( String iname )

{

// need to also use the .equals method here because you are

// not doing a boolean comparison you are comparing two

// string objects

///** if( iname == icat.getName() ) **



for (int i = 0; i < aKennel.length; i++) {



if(iname.equals(icat[i]))

{

myCatNumber--;

System.out.println( iname + " has been removed." );

/*also you will have to do some serious array manipulation here to resize your array and actually remove the cat object that the user chose. I suggest that if you are just trying to get this to work use an ArrayList rather than an array If you want to do it this way you will have to either use the copyArray method or create a "temp" array that does the actual remove and then iterate throught the array again and copy the left over values of the temp array into the aKennel array*/

}





}

System.out.println( "Sorry, we don't have any cats by that name." );

}

}





Plug away and try it out. It will take some time and some practice, but you have the right idea.



A good resource that has been there in the past for me when I was learning java is www.javaranch.com They have beginners and intermediates forums that you can post to and they are always more than eager to assist.



Hope this helps you out.
zhen zhen
2007-11-12 21:32:40 UTC
A declaration: Cat icat;

It is simply a line which states that you want to have a space for a variable of the class, "Cat" and the name of this variable is "icat". In other words, this is probably just declaring that you want a variable of a certain name.



Whereas an initialization is: Cat icat = new Cat();

It is where you actually create that particular object with the new keyword. Sometimes you also refer to this as "instantiate an object". In the case of variables like String or int, you initialize them when you type: String s = "something";



If it's a declaration only, then it would be: String s;
teef_au
2007-11-12 21:07:03 UTC
First time I see iname is on the faulty line. Where is it declared? Under your class you say Cat icat, but is this a declaration? I am assuming this is java a language I detest sorry.



teef_au.
sbben76
2007-11-12 21:14:40 UTC
It is warning you that iCat may not have been assigned a value during runtime. You must first initialize iCat using the new keyword
?
2016-05-23 02:06:10 UTC
"declared" and "initialized" are two slightly different things. To declare a variable is to make the variable NAME known to the program. To initialize it is to give it a default value and/or data type, such as 0 (integer). I think in this case C is a NULL, and not zero, and the interpreter doesn't like that. Note that A and B are defined by integers, but in the expression "int c = c + Number;", C (the second one) has no value. Try "int c = 0" before the line giving the error.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...