The local variable n may not have been initialized

后端 未结 4 1305
长发绾君心
长发绾君心 2021-01-27 23:35
import View.UI;
public class App {
UI m;    
public static void main(String [] args){
    System.out.println(\"Hello\");
    UI n ;
    n.menu();
}}

Th

相关标签:
4条回答
  • 2021-01-28 00:07
    public static void main(String [] args){
        System.out.println("Hello");
        UI n =AssignUIOBjectHere ;
        n.menu();
    }}
    

    Method's local variables needs to be initialize, before they are using.

    Compiler complaining that you are using a variable which is not yet initialized.

    As per language specification

    Every variable in a program must have a value before its value is used:

    0 讨论(0)
  • 2021-01-28 00:13

    If you are declaring variables/object inside the method in java you need to initialize it.

    In most simple term,

    In your case its an object which is accessing a method so if you do not initialize it like

    UI n = new UI();
    

    it would give you an NULL pointer exception.

    Hope it helps.

    0 讨论(0)
  • 2021-01-28 00:22

    You have to create an instance of UI, such as; UI n = new UI();

    0 讨论(0)
  • 2021-01-28 00:28

    As it is, calling n.menu() will cause a NullPointerException since you never assign anything to it.

    You will need to do something like so: UI n = new ... or UI n = someObject.

    0 讨论(0)
提交回复
热议问题