?
Solved

getting passed error:might not have bee initialized

Posted on 2003-02-26
7
Medium Priority
?
203 Views
Last Modified: 2010-03-31
When I initialize a variable inside a block but you that variable in another block it gives me the following error

error: might not have been initialized

how do I get around this at compile time?

thanks
Nick
0
Comment
Question by:nick5454
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 400 total points
ID: 8027659
set it = to null;

String s = null;

try {
  s = "blah";
} catch (Exception e) {}

(if it's a native type, you will have to set a default value (ie, true/false for boolean) b/c you can't assign null)



0
 
LVL 3

Expert Comment

by:allahabad
ID: 8028317
local variable is not initialized:
1. When method returns void.
    for ex.
   
   public void test(){
         String s;
     
           try{
     
              s="blah";
              System.out.println(s);
           }catch (Exception e){}
            return s;
     
   }
   
   This will compile without a problem.
   
 2. When method returns a Type.
    public String test(){
     String s;

       try{

          s="blah";
          System.out.println(s);
       }catch (Exception e){}
     return s;

    }
   Compiler will issue error "variable s might not have initialzed return s".
   

So initialze your local variable , if you are returning that variable from the method.
0
 
LVL 2

Expert Comment

by:warsql
ID: 8030696
what? return a String in a void method?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:allahabad
ID: 8030786
I am sorry; forgot to take out "return", while doing copy /paste.  
public void test(){
        String s;
   
          try{
   
             s="blah";
             System.out.println(s);
          }catch (Exception e){}
           
   
  }
 
0
 
LVL 1

Expert Comment

by:ykaganov
ID: 8030922
wrt bobbit31's post: If it's a native type, it will always be initialized to a default value (false for booleans, zero for integers, etc), and you won't even see this error.

Otherwise, setting it to null will work fine, just doublecheck your logic to make sure it does get initialized with the right values when you use it...

- Eugene
0
 
LVL 2

Expert Comment

by:warsql
ID: 8030949
easy to do ;-)
0
 

Author Comment

by:nick5454
ID: 8039222
not really sure who to give this one to, thier all great including the convo started. If EE could split them that would be nice also
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question