im having problem setting values into a String array.. it must be with a setter method (yes this is homework).

i have created a Person class which u can set name, age, gender and then i created a Teacher class which extends person and further allows you to set ( school teaching at, teacher ID, subjects teaching )

subjects teaching must be a String Array.

this is my setter method:
- subjectsTaught is my array declared at the begining

public void setSubTaught(int pos, String value)
    {
        subjectsTaught[pos]=value;
    }

this is my getter method:

public String getSubTaught()
    {
        String sub = Arrays.toString(subjectsTaught);
        int len = sub.length();
        String fix = sub.substring(1,len-1);
        return fix;
    }

this is in my main method:

Teacher teach = new Teacher("John Smith");
teach.setSubTaught(0, "TESTTT");

I try to display it using System.out.println(teach.getSubTaught())
but I keep getting a NullPointerException returning me to the setter method at the line:

subjectsTaught[pos]=value;

what am i doing wrong :S

Recommended Answers

All 5 Replies

In getSubTaught() you loop thru all the members of the array, regardless of how many have actually been populated. You only set element [0], so when the loop hits element [1] you get a null pointer.

the reason why i used Arrays.toString(subjectsTaught); was because it displays everything in the array with [test, test1, test2,] etc.. which was the format i needed without the [ ] so i substringed them out.... (another method you taught me)

is there a way to limit Arrays.toString() to only look through the actual size of the array?

You have to instantiate an array of string - subjectsTaught.

class Teacher extends Person 
 {
      ....

      private String []subjectsTaught;
      .....
      public Teacher(String name) {
            ....
            subjectsTaught=new String[10]; // Instantiate an array of String to store 10 subject names
            for(int i=0;i<10;i++) {
                  subjectsTaught[i]=""; // Empty string
            }
      }
     ...
     ...
public void setSubTaught(int pos, String value)
    {
     if(pos<=9)
          subjectsTaught[pos]=value;
    }
    ....
    ....
}

the reason why i used
is there a way to limit Arrays.toString() to only look through the actual size of the array?

Not that I know - if there are nulls in your array it will fail.
Maybe you would be better off with an ArrayList or Vector rather than a dumb old array, that way Java keeps track of the size for you. If you are committed to arrays, then something like this should work

String subjects = "";
for (String subject : subjectsTaught) {
   if (subject != null) subjects += subject;
}
return subjects;

ps

subjectsTaught is my array declared at the begining

declared, OK, but is it initialised? If not, you need something like the previous poster's suggestion as well, although personally I would prefer

private String [] subjectsTaught=new String[10];
private int subjectsCount = 0;
.....

public void setSubTaught(String value) {
if(subjectsCount <=9)
subjectsTaught[++subjectsCount ]=value;
}

well both your methods worked.. the problems:
adatapost method:

output = TEST, , , , ,
-commas even after null values.

james method:
output = TESTTESTTEST
-no spacing or commas at all.

with james's method i can fix it by doing "TEST ," (adding the space and comma manual).

i guess i'll just stick to doing that, works good enough for me... thanks for help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.