Variable might not have been initialized error
You declared them, but you didn't initialize them. Initializing them is setting them equal to a value:
int a; // This is a declaration
a = 0; // This is an initialization
int b = 1; // This is a declaration and initialization
You get the error because you haven't initialized the variables, but you increment them (e.g., a++
) in the for
loop.
Java primitives have default values but as one user commented below
Their default value is zero when declared as class members. Local variables don't have default values
variable result might not have been initialized
Your first line should be:
int n1, n2, result=0;
result
variable was not initialized nor it could be calculated from the code, hence the compiler thinks it won't find the value of the variable. Thus its giving compilation error.
How to avoid variable might not have been initialized near try/catch with System.exit
The oddity here is that the compiler doesn't know that System.exit(1);
will never return. If it knew that, it would be happy.
So all you need to do is give it something that it knows won't let you get from the catch block to after the try/catch. For example:
try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
System.out.println(USAGE);
System.exit(1);
throw new RuntimeError("Make sure the end of the catch block is unreachable");
}
If you need to do this often, you might want to write a helper method, and throw the result of it (which you'll never use). That way you still only have a single line of code for "I want to quit now".
try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
System.out.println(USAGE);
throw HelperClass.systemExit(1);
}
...
public class HelperClass {
public static RuntimeException systemExit(int exitCode) {
System.exit(1);
throw new RuntimeException("We won't get here");
}
}
Another option I've used quite a bit is to define a sort of "User error" exception. You can then catch that at the top level (in main
), print any message and possibly display the usage. That way:
- You can unit test user errors (unit testing
System.exit
is at least more awkward) - You have centralized handling of "what do I want to do if the user made an error" rather than including
System.out.println(USAGE)
in multiple places - You don't run into this definite assignment issue
So your code would then be:
try {
foo = Long.parseLong(args[0]);
bar = Long.parseLong(args[0]);
} catch (NumberFormatException e) {
throw new UserInputException("foo and bar must both be valid integers");
}
Why does this code keep returning a variable might not have been initialized error?
In the below code :
public static double calcPostage(double ounces)
{
double hold;
if ((ounces <= 10) && (ounces > 0))
hold = 3;
else if (ounces > 10)
hold = ((ounces-10)*0.15)+3;
else
System.out.print("Invalid input.");
return hold;
}
Suppose , if none of the conditions in "if" and "else if" are met, then -- double type variable hold --
doesn't gets initialized. Hence you are receiving the error :
Main.java:15: error: variable hold might not have been initialized
return hold;
^
So it is always better to initialize your variables to assign them a default value or an initial value.
Do this :
double hold = 0.0;
Related Topics
Java Generics Type Erasure: When and What Happens
How to Create a File and Write to It
How to Import the Javax.Servlet/Jakarta.Servlet API in My Eclipse Project
How to Read/Convert an Inputstream into a String in Java
Converting Iso 8601-Compliant String to Java.Util.Date
How to Parse Invalid (Bad/Not Well-Formed) Xml
What Is a Serialversionuid and Why Should I Use It
How to Add Local Jar Files to a Maven Project
Java Multiple File Transfer Over Socket
How to Efficiently Iterate Over Each Entry in a Java Map
What Is Reflection and Why Is It Useful
Manipulating an Access Database from Java Without Odbc
Add a Complex Image in the Panel, With Buttons Around It in One Customized User Interface
What Is the Reason Behind "Non-Static Method Cannot Be Referenced from a Static Context"
What Is the Recommended Way to Escape HTML Symbols in Plain Java
What Are the Reasons Why Map.Get(Object Key) Is Not (Fully) Generic