• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception on running  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody help me with this,please?
the code compiles with no errors but when i try to run it it gives me the next output:
[null0, null0, null0]
Exception in thread "main" java.lang.NullPointerException
at SortMountains$NameComparator.compare(SortMountains.java:8)
at SortMountains$NameComparator.compare(SortMountains.java:6)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:324)
at java.util.TimSort.sort(TimSort.java:189)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at SortMountains.go(SortMountains.java:26)
at SortMountains.main(SortMountains.java:17)
Java Result: 1

 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doesn't do what you need:


What it does is set the initial value (null or 0) to the method parameter.

Also, note that it is good practice for variables to begin with a lowercase letter in Java.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:This doesn't do what you need:


What it does is set the initial value (null or 0) to the method parameter.

Also, note that it is good practice for variables to begin with a lowercase letter in Java.


thank you for your reply
what should i do to make it work?
 
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the output of your toString method: [null0, null0, null0]

You have three objects in the list, but they all have a 'null' name.
That would explain the null pointer exception. But why is "name" null?

The toString method is using the name attribute of the Mountain class.
Why would this attribute have a value of null after calling your constructor?

A good habit to get into in a constructor is to assign variables with:
this.name = name. That way there is no confusion about which value you are setting and which is being set from.
 
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Completely agree with Stefan ..
Take a close look of the constructor of Mountain . The fix is there .

One quick question
Why you declared 3 inner classes ?

Yes one more thing dont use capital letter when declaring variable (here it is Height) ..

Satya
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
A good habit to get into in a constructor is to assign variables with:
this.name = name. That way there is no confusion about which value you are setting and which is being set from.

got it working.thank you
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:Completely agree with Stefan ..
One quick question
Why you declared 3 inner classes ?
Satya

first of all,thank you for your reply
the first two were comparator classes that are used to sort my list in a specific way.
the third one i put there because i was just in a hurry and wanted to test the code.i know i should have put that one in a separate class
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fady zohdy wrote: . . . i was just in a hurry . . .
Frequent cause of road accidents.

The big difference between dangerous driving and dangerous coding is that you can't harm 1000000 people simultaneously by dangerous driving.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!