Passing 2 D arrays in functions - nul pointer error

edited October 2016 in Questions about Code

I am reading a file with 2 columns and n number of rows. the number of rows will change depending on which image file I process. i want to read such a file, populate a 2D matrix to do further calculations. my code is as below. I get a nul pointer exception in line 23 where I assign the parsed value from the file o the matrix location. where am i going wrong?? String filelocation="filepath/input.txt"; int [][]inputmatrix;

void setup() { size(500,500); readfile(filelocation); printmatrix(inputmatrix); noLoop(); }

void draw() { }

void readfile(String location){ location = filelocation; String[]inputlines=loadStrings(filelocation); int totalrows = inputlines.length; println ("no of rowns =", totalrows); int rowno=0; for (int i=0;i<totalrows;i++){ String pieces[]=split(inputlines[i],' '); println(pieces[0],pieces[1]); inputmatrix[i][0]= int (pieces[0]);// LINE WITH NUL POINTER EXEPTION) //splittoken(inputlines,rowno); rowno++; } }

Tagged:

Answers

  • Answer ✓

    Format your post. Edit post, select code and press ctrl+o.

    The problem is that you didn't init your matrix. Add this in your function:

    inputmatrix = new int[totalrows][2];

    Kf

  • thanks Kfrajer. realized that arrays have to be initialised. sorry about the lack of format. this is my first time on the forum

Sign In or Register to comment.