Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

java - instantiated object's method won't run, error "variable(object's name) not initialized

/** 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 + "
");

    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*/
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change:

Performance band;

To:

Performance band = null;

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.

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

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...