4 Replies - 429 Views - Last Post: 20 November 2012 - 06:57 AM Rate Topic: -----

#1 Alias99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 12

Creating an Instance of SelectedTag

Posted 20 November 2012 - 01:24 AM

Hi, following is the code to create an Instance of class SelectedTag:


Tag[] array;
   
  array = new Tag[4];
  
  array[1] = new Tag();
  SelectedTag d = new SelectedTag(2,array);



But Line 4 throws error java.lang.NullPointerException.
May I know what is the nature of this error please?
Is This A Good Question/Topic? 0
  • +

Replies To: Creating an Instance of SelectedTag

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Creating an Instance of SelectedTag

Posted 20 November 2012 - 02:34 AM

The error means that the value you are trying to access is null or does not exist. From the little bit of code you have posted it looks as if you are trying to reference the 3rd element (2) of the array. When you initialise an array the values are assigned null. You create an instance of an object at array position 1 and then attempt to reference or call SelectedTag on what looks to be element two.

For starters your first initialsation generally should be at index zero, not at index 1.

Tag[] array = new Tag[4];
array[0] = new Tag();
SelectedTag d = new SelectedTag(0,array);



If you were to in this case do
SelectedTag d = new SelectedTag(1,array);



You would also encounter a NullPointerException as the value at position 1, i.e. the second element has not been initialised.

Regards
Was This Post Helpful? 2
  • +
  • -

#3 Alias99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 12

Re: Creating an Instance of SelectedTag

Posted 20 November 2012 - 05:40 AM

Even after using the modified the code, the same error results.
The instance selected Tag is from class best first search, weka.attributeSelection.BestFirst
and he is the modified code:

Tag[] array;
   
  array = new Tag[4];
  
  array[0] = new Tag(0,"backward");
  array[1] = new Tag(1,"forward");
  array[2] = new Tag(2,"hybrid");
  SelectedTag d;
  d = new SelectedTag(1,array);



Was This Post Helpful? 0
  • +
  • -

#4 Alias99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-October 12

Re: Creating an Instance of SelectedTag

Posted 20 November 2012 - 06:11 AM

Thank you so much pbl, understood the source of the error, the code functions well now.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,094
  • Joined: 05-April 11

Re: Creating an Instance of SelectedTag

Posted 20 November 2012 - 06:57 AM

pbl? where did pbl answer this topic =o

There is one thing I don't understand
Why don't you pass a Tag object to the constructor?
new SelectedTag(array[0]); //Pass a Tag instead of 1, array



What exactly is the SelectedTag class doing?
Couldn't it be enough to have a
Tag selectedTag = array[0];


This post has been edited by CasiOo: 20 November 2012 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1