我正在使用Processing(一种非常类似于Java的语言)创建一个程序,它将使用python排序的数据转换为文本文件,然后添加到处理草图中并以可滚动的气泡显示它们。我希望每次释放鼠标按钮时数组索引都会增加1,这样所有歌曲(即我排序的数据)都可以通过单击和释放来查看。问题是,即使我的draw()
和setup()
方法都是公共的,处理也不会让我使用我在draw()
方法中加载文件的数组。它给我的错误是第82行NullPointerException
:
text(songsort[a1],close,15,99,99);
它还表示我的文件在setup()
中可以读取时丢失且无法读取。我非常感谢你的帮助。
这是我的代码:
import de.looksgood.ani.*;
import de.looksgood.ani.easing.*;
PFont f;
int x=50;
int i=50;
int y=50;
int far=999;
int mid=666;
int close=333;
int end=1333;
int y1=113;
int far2=999;
int mid2=666;
int close2=333;
int end2=1333;
int y2=339;
int far3=999;
int mid3=666;
int close3=333;
int end3=1333;
int y3=567;
int a1=1;
int a2=2;
int a3=3;
int a4=4;
int b1=1;
int b2=2;
int b3=3;
int b4=4;
int c1=1;
int c2=2;
int c3=3;
int c4=4;
public void setup(){
size(1333,680);
smooth();
f = createFont("Georgia", 32);
textFont(f);
textAlign(CENTER, CENTER);
String[] songsort= loadStrings("songsort.txt");
String[] artistsort= loadStrings("yearsort.txt");
String[] yearsort= loadStrings("artistsort.txt");
println("There are "+songsort.length+artistsort.length+yearsort.length+" lines");
Ani.init(this);
Ani.setDefaultEasing(Ani.QUART_IN_OUT);
}
public void draw(){
background(169);
fill(123,43,23);
stroke(0);
rect(x,1,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by year",x,15,100,height/3);
fill(123,43,23);
stroke(0);
ellipse(far,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(mid,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(close,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(end,y1,99,99);
textFont(f);
textSize(16);
fill(255);
text(songsort[1],close,15,99,99); ///////////// Error here! /////////////
if((mouseX<1333) && (mouseX>0) && (mouseY>0) && (mouseY<226) && (mousePressed==true)){
x=x-5;
end=end-5;
far=far-5;
mid=mid-5;
close=close-5;
}
fill(43,123,23);
stroke(255);
rect(i,226.66,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by song name",i,226.66,100,height/3);
fill(43,123,23);
stroke(255);
ellipse(far2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(mid2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(close2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(end2,y2,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>226.66) && (mouseY<453.34) && (mousePressed==true)){
i=i-5;
end2=end2-5;
far2=far2-5;
mid2=mid2-5;
close2=close2-5;
}
fill(240,179,93);
stroke(147);
rect(y,453.34,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by artist",y,453.34,100,height/3);
fill(240,179,93);
stroke(147);
ellipse(far3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(mid3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(close3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(end3,y3,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>453.34) && (mouseY<680) && (mousePressed==true)){
y=y-5;
end3=end3-5;
far3=far3-5;
mid3=mid3-5;
close3=close3-5;
}
}
void mouseReleased(){
Ani.to(this, 1.0, "close", 133);
Ani.to(this, 1.0, "mid", 466);
Ani.to(this, 1.0, "far", 799);
Ani.to(this, 1.0, "end", 1133);
Ani.to(this, 1.0, "close2", 133);
Ani.to(this, 1.0, "mid2", 466);
Ani.to(this, 1.0, "far2", 799);
Ani.to(this, 1.0, "end2", 1133);
Ani.to(this, 1.0, "close3", 133);
Ani.to(this, 1.0, "mid3", 466);
Ani.to(this, 1.0, "far3", 799);
Ani.to(this, 1.0, "end3", 1133);
}
谢谢!
答案 0 :(得分:2)
您的数组在函数设置()中被声明为局部变量,因此它们在此函数之外是不可见的。要使它们在类的其他函数中可见,您应该将它们声明为此类的字段。
public class YourClass {
private String[] songsort;
private String[] artistsort;
private String[] yearsort;
public void setup() {
...
songsort = loadStrings("songsort.txt");
artistsort = loadStrings("yearsort.txt");
yearsort = loadStrings("artistsort.txt");
...
}
}
答案 1 :(得分:1)
以下是您的代码的精简版本:
//0
public void setup() { //1
String[] songsort = loadStrings("songsort.txt"); //2
} //3
//4
public void draw() { //5
text(songsort[1],close,15,99,99); //6
} //7
在第2行,您使用songsort
声明变量String[] songsort
。因为这是方法setup
,变量songsort
是“局部变量”,只能在该方法中访问。要解决此问题,在第0行声明变量,但仍然在<{1}}方法中初始化,如下所示:
setup
对所有String[] songsort; //0
public void setup() { //1
songsort = loadStrings("songsort.txt"); //2
} //3
//4
public void draw() { //5
text(songsort[1],close,15,99,99); //6
} //7
执行此操作并将其应用于您拥有的代码应如下所示:
String[]
变量import de.looksgood.ani.*;
import de.looksgood.ani.easing.*;
String[] songsort;
String[] artistsort;
String[] yearsort;
PFont f;
int x=50;
int i=50;
int y=50;
int far=999;
int mid=666;
int close=333;
int end=1333;
int y1=113;
int far2=999;
int mid2=666;
int close2=333;
int end2=1333;
int y2=339;
int far3=999;
int mid3=666;
int close3=333;
int end3=1333;
int y3=567;
int a1=1;
int a2=2;
int a3=3;
int a4=4;
int b1=1;
int b2=2;
int b3=3;
int b4=4;
int c1=1;
int c2=2;
int c3=3;
int c4=4;
public void setup() {
size(1333,680);
smooth();
f = createFont("Georgia", 32);
textFont(f);
textAlign(CENTER, CENTER);
songsort= loadStrings("songsort.txt");
artistsort= loadStrings("yearsort.txt");
yearsort= loadStrings("artistsort.txt");
println("There are "+songsort.length+artistsort.length+yearsort.length+" lines");
Ani.init(this);
Ani.setDefaultEasing(Ani.QUART_IN_OUT);
}
public void draw() {
background(169);
fill(123,43,23);
stroke(0);
rect(x,1,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by year",x,15,100,height/3);
fill(123,43,23);
stroke(0);
ellipse(far,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(mid,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(close,y1,99,99);
fill(123,43,23);
stroke(0);
ellipse(end,y1,99,99);
textFont(f);
textSize(16);
fill(255);
text(songsort[1],close,15,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>0) && (mouseY<226) && (mousePressed==true)) {
x=x-5;
end=end-5;
far=far-5;
mid=mid-5;
close=close-5;
}
fill(43,123,23);
stroke(255);
rect(i,226.66,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by song name",i,226.66,100,height/3);
fill(43,123,23);
stroke(255);
ellipse(far2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(mid2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(close2,y2,99,99);
fill(43,123,23);
stroke(255);
ellipse(end2,y2,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>226.66) && (mouseY<453.34) && (mousePressed==true)) {
i=i-5;
end2=end2-5;
far2=far2-5;
mid2=mid2-5;
close2=close2-5;
}
fill(240,179,93);
stroke(147);
rect(y,453.34,100,height/3);
textFont(f);
textSize(32);
fill(50);
text("sorted by artist",y,453.34,100,height/3);
fill(240,179,93);
stroke(147);
ellipse(far3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(mid3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(close3,y3,99,99);
fill(240,179,93);
stroke(147);
ellipse(end3,y3,99,99);
if((mouseX<1333) && (mouseX>0) && (mouseY>453.34) && (mouseY<680) && (mousePressed==true)) {
y=y-5;
end3=end3-5;
far3=far3-5;
mid3=mid3-5;
close3=close3-5;
}
}
void mouseReleased() {
Ani.to(this, 1.0, "close", 133);
Ani.to(this, 1.0, "mid", 466);
Ani.to(this, 1.0, "far", 799);
Ani.to(this, 1.0, "end", 1133);
Ani.to(this, 1.0, "close2", 133);
Ani.to(this, 1.0, "mid2", 466);
Ani.to(this, 1.0, "far2", 799);
Ani.to(this, 1.0, "end2", 1133);
Ani.to(this, 1.0, "close3", 133);
Ani.to(this, 1.0, "mid3", 466);
Ani.to(this, 1.0, "far3", 799);
Ani.to(this, 1.0, "end3", 1133);
}
,songsort
和artistsort
应该是“字段”,可以在整个标签中访问,在本例中为草图。