【发布时间】:2015-01-10 16:32:02
【问题描述】:
当我尝试编译这段代码时,我得到了这个错误:
dn09.java:38: error: illegal start of expression
public Tip[] preberi (Scanner sc) {
^ dn09.java:38: error: ';' expected
public Tip[] preberi (Scanner sc) {
^ dn09.java:38: error: ';' expected
public Tip[] preberi (Scanner sc) {
^ 3 errors
[Napaka | process.javac]: Object reference not set to an instance of an object.
这是问题中的代码行:
public Tip[] preberi(Scanner sc) {
Tip[] tipi = new tipi[d];
for (int i = 0; i < tipi.length; i++) {
String tip = sc.next();
switch (tip) {
case "prim":
tipi[i] = new Prim(sc.nextInt());
break;
case "arr":
tipi[i] = new Arr(sc.nextInt(), sc.nextInt());
break;
case "ostruct":
break;
case "pstruct":
break;
}
}
return tipi;
}
我在main() 方法中声明了我的Scanner,它已被导入以及所有内容。
正如你们中的一些人所问的那样,这是我的全部代码(它根本没有处于工作状态,因为你也会看到我是一个初学者,所以它非常简单。
public class dn09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int b = sc.nextInt();
int d = sc.nextInt();
Tip[] tipi = preberi(sc);
int u = sc.nextInt();
int[] ukazi = new int[u];
for (int i = 0; i < u; i++) {
ukazi[i] = sc.nextInt(); //if you know a better way to store 2 numbers where i could then
//use the numbers separately that would be super helpfull as id
} //need it for 2 switch statements which im currenty trying to
for (int i = 0; i < u; i++) {//fit into 1.
switch(ukazi[i]) {
case 11:
break;
case 12:
break;
case 13:
break;
case 21:
break;
case 22:
break;
case 23:
break;
case 31:
break;
case 32:
break;
case 33:
break;
}
}
public Tip[] preberi(Scanner sc) {
Tip[] tipi = new tipi[d];
for (int i = 0; i < tipi.length; i++) {
String tip = sc.next();
switch (tip) {
case "prim":
tipi[i] = new Prim(sc.nextInt());
break;
case "arr":
tipi[i] = new Arr(sc.nextInt(), sc.nextInt());
break;
case "ostruct":
break;
case "pstruct":
break;
}
}
return tipi;
}
}
private static class Prim extends dn09 {
protected int v;
public static Prim (int v) {
this.v = v;
}
}
private static class Arr extends dn09 {
protected int n;
protected int t;
public static Arr (int n, int t) {
this.t = t;
this.n = n;
}
}
}
【问题讨论】:
-
您的
main方法中是否也声明了此方法? -
粘贴整个 dn09 类代码。
-
问题似乎出在您发布的代码之前。发布一个可运行的最小示例。此外,通常您应该使用像 Eclipse 这样的优秀 IDE 来帮助解决编译时错误。
-
您要切换数组 (
switch (tipi))?这行不通。 -
因为
public Tip[] preberi(Scanner sc)方法是 insidemain方法。那是行不通的。