We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Portfolio UI - Null Pointer Exception
Page Index Toggle Pages: 1
Portfolio UI - Null Pointer Exception (Read 1008 times)
Portfolio UI - Null Pointer Exception
Mar 29th, 2008, 6:50pm
 
I'm new to Processing and I'm trying to make a portfolio with it. I'm not entirely sure how classes work.

What I'm trying to do is create a portfolio, using an MVC. I'm not sure if processing can to this but I want to have each section(gallery, main page, about me) be it's own class.

this is what I've managed to finagle out for code. When I run it, I get an Null Pointer Exception and my background image doesn't get drawn.

import processing.candy.*;
import processing.xml.*;
int screenSelector;

SVG loginImage; //background Image

Login lPage = new Login(loginImage);

void setup()  {
 size(1024, (screen.height * 3)/4);
 colorMode(RGB, 255, 255, 255, 100);
 background(255);
 PFont font;
 font = loadFont("BankGothicBT-Medium-18.vlw");
 textFont (font, 18);
 noLoop();
 screenSelector = 0;

 SVG loginImage = new SVG(this, "bluephoenixlogin.svg");
}

class Login  {
 SVG Image;
 Login(SVG loader)  {
   Image = loader;
 }

 void draw()  {
   int ycenter = height / 2;
   int xcenter = width / 2;
   smooth();
   //Null Pointer Exception on the line below
   Image.draw((xcenter - 402), (ycenter - 271));
 }
}

//the controller, it will get passed a variable that will
//determine which case(section) is drawn.
void draw()  {
 switch(screenSelector)  {
 case 0:
   lPage.draw();
   redraw();
 }
}
Re: Portfolio UI - Null Pointer Exception
Reply #1 - Mar 29th, 2008, 9:08pm
 
You create the instance of your login class before you load the image. So you this part :
Code:

SVG loginImage; //background Image
Login lPage = new Login(loginImage); //where loginImage = null

create an instance where the local Image variable is null.

So you have to create the instance after loading the image. By the way the image doesn't need to be global, cause it's holding in the Login object.

Code:


import processing.candy.*;
import processing.xml.*;
int screenSelector;

Login lPage;

void setup() {
size(1024, (screen.height * 3)/4);
colorMode(RGB, 255, 255, 255, 100);
background(255);
textFont (loadFont("BankGothicBT-Medium-18.vlw"), 18);
noLoop();
smooth();
screenSelector = 0;
lPage = new Login(new SVG(this, "bluephoenixlogin.svg"));
}

class Login {
SVG Image;
Login(SVG loader) {
Image = loader;
}

void draw() {
int ycenter = height / 2;
int xcenter = width / 2;
Image.draw((xcenter - 402), (ycenter - 271));
}
}

//the controller, it will get passed a variable that will
//determine which case(section) is drawn.
void draw() {
switch(screenSelector) {
case 0:
lPage.draw();
redraw();
}
}
Page Index Toggle Pages: 1