1
votes

I am trying to open an image that is placed at path "sketchfolder/data/1024x768/gulli2.png". Its working fine on windows but its not working on Android and giving error. Here is the error:

Could not find the image \1024x768\gulli2.png. FATAL EXCEPTION: Animation Thread Process: processing.test.exercise_5_3_noisechain, PID: 11878 java.lang.NullPointerException at processing.test.exercise_5_3_noisechain.Exercise_5_3_NoiseChain$Gulli.(Exercise_5_3_NoiseChain.java:112) at processing.test.exercise_5_3_noisechain.Exercise_5_3_NoiseChain.setup(Exercise_5_3_NoiseChain.java:37) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:841)

The code I am trying:

class Gulli {
  Body body;
  float w;
  float h;
  float angle;
  PImage img;

  Gulli(float x_, float y_) {
    float x = x_;
    float y = y_;
    w = (float)(width*2.2/100.0);
    h = w/2;
    img = loadImage("\\1024x768\\gulli2.png");
    img.loadPixels();
    img.resize((int)w*2,0);
    img.updatePixels();
    angle = 0;
    makeBody(new Vec2(x, y), w, h, angle);
  }

  void makeBody(Vec2 center, float w_, float h_, float a) {
    BodyDef bd = new BodyDef();
    bd.position.set(box2d.coordPixelsToWorld(center));
    bd.setAngularVelocity(a);
    bd.type = BodyType.DYNAMIC;
    bd.bullet = true;
    body = box2d.createBody(bd);
    PolygonShape sd = new PolygonShape();
    Vec2 []vertices = new Vec2[6];
    vertices[0] = new Vec2(box2d.scalarPixelsToWorld(w), 0);
    vertices[1] = new Vec2(box2d.scalarPixelsToWorld(h), box2d.scalarPixelsToWorld(h/3));
    vertices[2] = new Vec2(box2d.scalarPixelsToWorld(-h), box2d.scalarPixelsToWorld(h/3));
    vertices[3] = new Vec2(box2d.scalarPixelsToWorld(-w), 0);
    vertices[4] = new Vec2(box2d.scalarPixelsToWorld(-h), box2d.scalarPixelsToWorld(-h/3));
    vertices[5] = new Vec2(box2d.scalarPixelsToWorld(h), box2d.scalarPixelsToWorld(-h/3));
    sd.set(vertices, vertices.length);
    FixtureDef fd = new FixtureDef();
    fd.shape = sd;
    fd.density = 4.0;
    fd.friction = 0.6;
    fd.restitution = 0.3;
    body.createFixture(fd);
    bd.allowSleep = false;
    body.setUserData(this);
  }

  void display() {
    Vec2 pos = box2d.getBodyPixelCoord(body);
    float a = body.getAngle();
    Fixture f = body.getFixtureList();
    PolygonShape ps = (PolygonShape) f.getShape();
    pushMatrix();
    translate(pos.x, pos.y);
    rotate(-a);
    image(img, 0, 0);
    popMatrix();
  }
} 


import shiffman.box2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;

Box2DProcessing box2d;
Gulli gulli;

void setup() {
//  size(1024, 768);  // turn on for windows
  box2d = new Box2DProcessing(this);
  box2d.createWorld();
  gulli = new Gulli((float)width*15.0/100.0, (float)height*95.0/100.0);    
  smooth();
}

void draw() {
  gulli.display();
  box2d.step();
}

Plz help.... Thank you..

1
Where is line 12 of Exercise_5_3_NoiseChain.java? - Code-Apprentice
sorry my mistake. i forgot to mention display() function of gulli. i have mentioned it now. plz check now. thanks :) - user3026927
which line are u talking about? is there any error? - user3026927
(Exercise_5_3_NoiseChain.java:112) This part of the error message means that the problem occurs on line 112 of Exercise_5_3_NoiseChain.java...oops, I typed 12 earlier...so which line of code is 112? - Code-Apprentice
there is no line 112. The error is where i am loading image. the path which i have given is not working on android. but its working fine on windows - user3026927

1 Answers

0
votes

Your log shows a NullPointerException error, this means you are trying to point to a non-existent image. Try to include the entire path. And without the double slashes "//". If you are working with URI formats, then it could be that the control doesn't support these formats. If that is the case, download the image first, and then load it.