• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing formatted time in a String array.  RSS feed

 
Laura kiwi
Greenhorn
Posts: 3
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new at java and i'm preparing for the OCA exam. To practise the use of different API's i'm trying to build a to do list per day and hour.
So i wanted to make an array with the hours and the corresponding activities. My approach to this was to create a LocalTime object and use DateTimeFormatter and than assign it to a new string. This doesn't seem to work, when ik run the program i keep getting a nullpointerexception that is pointing to the activities[0][0] = formattedTime; line.  However when i comment the last two lines out and replace them with; System.out.println(formattedTime); the output is 6 30, as expected. Could any one tell me why this doens't work and why i'm getting this nullpointerexception?

My code is below;

Thanks
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Code snippet you showed us, works fine for me and does not give any compile time errors nor exceptions during the run-time.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm getting this nullpointerexception? 

What variable has the null value?  Look at the stack trace to get the statement number then look at that statement to see what variable has the null value.  Add a print statement to print the variables' values if you can't tell which variable is  null.
 
Laura kiwi
Greenhorn
Posts: 3
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, i figured it out, i posted this String[][] activities = new String[10][10];  But i changed it when i posted it, because i thought it looked silly. So the nullpointerexception was because of the following line;String activities [10][], so when i changed it to [10][10] it worked fine. I thought that multidimensional arrays allow you to not define the number of elements in each array, but i think that i mistaked that for the following;
int [][] args = new int[4][];
args[0] = new int[5];
args[1] = new int[3]

Sorry for asking this maybe unnecessary question, but thanks for the answers anyway!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!