我是初学者,我正在编写此代码以进行作业。
我知道这看起来像重复,但知道NullPointerException错误意味着什么,我真的不明白如何修复这个,但我认为它与我导入的库有关;我真的不确定。我真诚地感谢任何建议。
第一部分是我的代码,下面是来自堆栈跟踪的NullPointerException错误。
如果我需要指明其他任何内容,请告诉我。
代码:
import processing.video.*;
String PATH = "/Project_2_Scetch/data/video_library/img4.mp4";
Movie mov;`enter code here`
PFont f;
Capture cam;
float xin, yin;
int maxImages = 8;
int imageIndex = 0;
int value = 0;
PImage img;
PImage[] images = new PImage[maxImages];
float positionX = xin;
float positionY = yin;
void setup() {
size(1000, 650);
smooth();
noStroke();
img = loadImage("img0.jpg");
rectMode(CENTER);
f = createFont("Source Sans Pro", 20, true);
if (key == 's'){
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
cam.start();
}
for (int i = 0; i < images.length; i ++ ) {
images[i] = loadImage( "img" + i + ".jpg" );}}
}
void movieEvent(Movie m) {
m.read();
}
void draw() {
background(50, 20, 120);
{
textSize(20);
text("In this game, there is no such thing as sleep. You will be going about my", 150, 70);
text("endless day either procrastinating, or catching up on homework", 200, 110 );
fill(255);
text("To procrastinate for endless fun use Free Mode", 288, 150);
fill(255);
text("To catch up on my homework and experience existential terror, use Adventure Mode", 95, 190);
fill(255);
text("Free Mode (Press J)", 175, 338);
fill(255);
text("Adventure Mode (Press K)", 579, 340);
fill(255);}
if (key == 'j') {
background(50, 20, 120);
text("To take intentionally terrible selfies, press the (A) key.", 150, 300);
fill(255);
text("To rescue pets, press the (B) key.", 150, 350);
fill(255);}
if (key == 'a') {
img = loadImage("img1.jpg");
image(img, width/4, height/6);
background(50, 20, 120);
text("If you would like to continue taking terrible selfies, press the (G) key.", 125, 320);
text("If you would like to go back and do something else, press the (J) key.", 125, 350);
}
if (key == 'b'){
img = loadImage("img6.jpg");
background(50, 20, 120);
text("If you would like to see more animals, press the (C) key.", 175, 320);
text("If you would like to go back and do something else, press the (J) key.", 175, 370);
}
if (key == 'c'){
image(mov, 0, 0);
}
if (key == 'e'){
// img = loadImage("img5.jpg");
background(50, 20, 120);
text("You seem like an animal lover. If you would like to watch Animal Planet, press the (D) key.", 50, 320);
text("If you are not an animal lover, how dare you.", 50, 360);
text("Press the spacebar to start over and rethink your life's choices.", 50, 390);
}
if (key == 'g'){
img = loadImage("img7.jpg");
background(50, 20, 120);
text("You seem to like taking selfies. If you would like to take some more funky fresh selfies,", 53, 300);
text("then please hit the (H) key.", 53, 350);
}
if (key == 'h'){
background(50, 20, 120);
img = loadImage("img4.jpg");
text("Are you satisfied with this selfie? Would you like to take more selfies? If so, press the (M) key.", 40, 300);
text("If not, then that's okay too. If you'd rather work on some homework, press the (K) key instead.", 40, 335);
}
if (key == 'm'){
text("Hey. You like selfies. No judgement here. Press the (S) key and take your own selfies and keep them close.", 40, 300);
text("If not, that's alright too. If you'd rather, press (K) to start on some homework :)", 40, 330);
}
if (key == 'd'){
background(50, 20, 120);
fill(50, 60, 170);
text("Go on, click on the box, you animal!", 250, 160);
fill(255);
line(470, 0, 470, 530);
line(0, 295, 355, 295);
}
//this is a hot mess too, but move onto the next thing.j
if (key == 's'){
if(cam.available() == true) {
cam.read();
}
image(cam, 0, 0);
// The following does the same, and is faster when just drawing the image
// without any additional resizing, transformations, or tint.
//set(0, 0, cam);
}
}
void mouseClicked() {
if (mouseX < 470 && mouseY < 295){
fill(255);
rect(470, 295, 500, 340);
link("http://www.animalplanet.com/tv-shows/");
}
}
NullPointerException错误#1:
当我执行if(key =='c')时会显示;功能,我正在尝试播放视频。我为此功能导入了视频库。
Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PGraphics.image(PGraphics.java:3765)
at processing.core.PApplet.image(PApplet.java:12109)
at Project_2_Scetch.draw(Project_2_Scetch.java:113)
at processing.core.PApplet.handleDraw(PApplet.java:2399)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
NullPointerException错误#2: 当我执行if(key =='s')时会显示;功能,在这里我试图让用户的网络摄像头拉起来。我为此功能导入了IPCapture库。
Exception in thread "Animation Thread" java.lang.NullPointerException
at Project_2_Scetch.draw(Project_2_Scetch.java:152)
at processing.core.PApplet.handleDraw(PApplet.java:2399)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
答案 0 :(得分:1)
您有两个不能初始化的变量:mov
和cam
。
mov
变量的初始化看起来像是你应该完成的任务。在使用该变量之前,您必须完成该部分。
您已初始化cam
变量,但它在if语句中。但请注意,永远不会输入if语句,因为在setup()
函数完成之后才会检测到用户输入。
另请注意,您不应该从loadImage()
功能调用draw()
。这将每秒加载图像60次,这将减慢你的草图速度。相反,在setup()
函数中加载所有图像是草图的开头。将每个图像存储在变量中,然后在以后需要时使用这些变量。