Results 1 to 2 of 2
  1. #1
    fneeks is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default cant print 2d array(giving null values)

    hi i have a problem that may be very easy for someone to solve..
    any ideas or suggestions is appreciated.
    ok i have created a 2d array of string type in a void method, now the problem is when i try to view this array in another class or another method in the same class i get all null values, but i can print the values in the same method so i know im assigning values into the array properly here's my code..

    public class MyTimeTable {
    public String[][] myData;
    String[] singleHdSchedule; ;
    public void createTimeTable()
    {
    singleHdSchedule= new String[7];
    myData= new String[3][7];
    myData[0][0] = "bob";
    myData[1][0] = "ron";
    myData[2][0] = "kelly";

    for (int i=0;i<3;i++){
    int myTime;
    myTime = 9;
    for (int j=1;j<7;j++)
    {
    myTime += 1;
    if (myTime>12)
    {
    myTime = myTime % 12 + 1;
    }
    singleHdSchedule[j] = Integer.toString(myTime);
    myData[i][j] = singleHdSchedule[j];
    }

    }
    for (int i=0;i<3;i++)
    {
    for (int j=0;j<7;j++)
    {
    System.out.print(myData[i][j]+ " ");
    }
    System.out.println();
    }

    }
    this is the output..
    bob 10 11 12 2 3 4
    ron 10 11 12 2 3 4
    kelly 10 11 12 2 3 4

    so bob works 10-4 ...

    so here i try to change a value in the 2d array and it gives me nullpointerException.

    public void changeTheValues()
    {
    for (int i= 0;i<3;i++)
    {
    for (int j=0;j<7;j++)
    {
    myData[i][j] = "12"; // this is giving nullpointerException.... why is //the value in myData[i][j] (null)?
    }
    }
    }

    so im calling these methods in another class like so..
    public static void main(String[] args)
    {
    MyTimeTable myTime = new MyTimeTable ();
    myTime.createTimeTable();
    myTime.changeTheValues(); //this line gives the nullpointerException
    //why am i unable to change the value here?
    //how can i change the value in the 2d array?
    }



    but when i try to re-assign values in the 2d array(in another method in the same class/any other class), the 2d array outputs all null values.
    i wanna be able to change the values of this 2d array in other areas(methods, classes) but it just gives me null values .. what is wrong with my logic ?
    how can i achieve this ?
    am i declaring the 2d array in the wrong place ?
    any help would be appreciated.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by fneeks View Post
    any help would be appreciated.

    in compiled and run your code without exceptions. i inserted this code just before myData[i][j] = "12";

    System.out.println(myData[i][j] + " changed to " + 12);

    and i got this output

    Java Code:
    bob 10 11 12 2 3 4 
    ron 10 11 12 2 3 4 
    kelly 10 11 12 2 3 4 
    bob changed to 12
    10 changed to 12
    11 changed to 12
    12 changed to 12
    2 changed to 12
    3 changed to 12
    4 changed to 12
    ron changed to 12
    10 changed to 12
    11 changed to 12
    12 changed to 12
    2 changed to 12
    3 changed to 12
    4 changed to 12
    kelly changed to 12
    10 changed to 12
    11 changed to 12
    12 changed to 12
    2 changed to 12
    3 changed to 12
    4 changed to 12
    and not exceptions were thrown.

Similar Threads

  1. null values
    By jabo in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 04:44 PM
  2. resting text field values on jsp page to null......
    By Sachin. in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 03:24 AM
  3. How to get null values stored in array
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 05-21-2009, 11:29 PM
  4. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 09:15 PM
  5. Null array when passed to MouseListener
    By stevemcc in forum New To Java
    Replies: 2
    Last Post: 04-02-2008, 11:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •