实例化对象的方法将无法运行,错误“变量(对象的名称)未初始化" [英] instantiated object's method won't run, error "variable(object's name) not initialized

查看:87
本文介绍了实例化对象的方法将无法运行,错误“变量(对象的名称)未初始化"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/**性能类的预期方法调用似乎存在问题.该对象被识别为band,但是由于某种原因,当我调用band.begin()(band是新的性能对象并开始其方法)时,band无法识别,并建议将其初始化为变量? */

/** There seems to be a problem with the intended method call from the performance class. The object is recognized as band but for some reason when i call band.begin() (band is the new performance object and begin its method) band is not recognized and it suggests it be initialized as a variable? */

import java.util.Scanner;


public class Music {

public static Instrument assignInstrument () {
    String instrumentChoice;
    String name;
    Scanner input = new Scanner (System.in);


    System.out.println("Select an instrument for the band member. ");
    System.out.print("Vocals, piccolo, Clarinet, Cymbal, or Drum: ");
    instrumentChoice = input.nextLine();
    System.out.print("Enter the band member's name: ");
    name = input.nextLine();
    if (instrumentChoice.equalsIgnoreCase("v")) {
        return (new Vocal(name));
    }   else if (instrumentChoice.equalsIgnoreCase("p")) {
        return (new Piccolo(name));
    }   else if (instrumentChoice.equalsIgnoreCase("cl")) {
        return (new Clarinet(name));
    }   else if (instrumentChoice.equalsIgnoreCase("cy")) {
        return (new Cymbal(name));
    }   else {
        return (new Drum(name));
    }
  }

/** I instantiated a "band" performance object but it is not recognizing it when i call its method... */
  public static void main (String [] args){
    Performance band; // band object instantiated
    Instrument bandmember1, bandmember2, bandmember3, bandmember4;
    Scanner input = new Scanner (System.in);
    String performanceChoice;

    bandmember1 = assignInstrument();
    bandmember2 = assignInstrument();
    bandmember3 = assignInstrument();
    bandmember4 = assignInstrument();
    System.out.println(bandmember1 + " " + bandmember2 + " " + bandmember3 + " " +     bandmember4 + "\n");

    System.out.print("Would you like to hear a solo, duet, trio, quartet, or leave?:  ");
    performanceChoice = input.nextLine();
    while (!performanceChoice.equalsIgnoreCase("l")) { // choosing the correct form of object
        if (performanceChoice.equalsIgnoreCase("s")) {
            band = new Performance (bandmember1);
        }   else if ( performanceChoice.equalsIgnoreCase("d")) {
            band = new Performance (bandmember1, bandmember2);
        }   else if ( performanceChoice.equalsIgnoreCase("q")) {
            band = new Performance (bandmember1, bandmember2, bandmember3);
        }   else {
            band = new Performance (bandmember1, bandmember2, bandmember3, bandmember4);
        }
    }
            band.Begin(); // error message: "variable band might not have been initialized".? 
 }
}


public class Performance {
private String arrangement;
private Instrument solo;
private Instrument duet_1, duet_2;
private Instrument trio_1, trio_2, trio_3;
private Instrument quart_1, quart_2, quart_3, quart_4;






public Performance (Instrument s) {
    solo = s;
    arrangement = solo.makeSound();
}

public Performance (Instrument d1, Instrument d2){
    duet_1 = d1;
    duet_2 = d2;
    arrangement = duet_1.makeSound() + duet_2.makeSound();
}

public Performance (Instrument t1, Instrument t2, Instrument t3){
    trio_1 = t1;
    trio_2 = t2;
    trio_3 = t3;
    arrangement = trio_1.makeSound() + trio_2.makeSound() + trio_3.makeSound();
}

public Performance (Instrument q1, Instrument q2, Instrument q3, Instrument q4){
    quart_1 = q1;
    quart_2 = q2;
    quart_3 = q3;
    quart_4 = q4;
    arrangement = quart_1.makeSound() + quart_2.makeSound() + quart_3.makeSound() + quart_4.makeSound();
}

public void Begin() {
    System.out.print(arrangement);
}

public String toString(){
    String program = "The performance includes ";
    program += arrangement;
    return program;
}
}
   /** Any suggestions? Thanks in advance*/

推荐答案

更改:

Performance band;

收件人:

Performance band = null;

之所以会这样,是因为编译器不确定该变量是否曾经被赋值(如果while不会首先传递).尽管可以使用空值(在某些情况下会导致NPE),但不能使用未分配的值.

This happens, since the compiler isn't sure the variable is ever assigned a value (if the while won't pass in first place). Although you could work with null values (resulting in NPEs in some cases), you can't work with unassigned values.

回想一下,全局变量会自动分配为null,而不是局部变量.

Recall, a global variable is automatically assigned to null, but not local variables.

请注意,即使在逻辑上必须对其进行分配(例如在try-catch块中),您也必须在try之前初始化并定义(可能是null),这是正确的,以便使用它,因为编译器不确定它是否会在try中分配,而是捕获异常.即使您在catch块中调用System.exit(1).这意味着,在逻辑上必须对其进行定义(否则程序将终止),但是编译器不是 so smart;)

Note this is true even if logically it must be assigned, for example in a try-catch block, you'll have to initialize and define ( probably null) before the try, in order to use it, since the compiler is not sure it will ever be assigned in the try, but rather catch an exception. Even if you call System.exit(1) in the catch block. That means, logically it must be defined (otherwise the program will terminate) but the compiler is not so smart ;)

这篇关于实例化对象的方法将无法运行,错误“变量(对象的名称)未初始化"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆