• Post Reply Bookmark Topic Watch Topic
  • New Topic

please help resolve the problem while running the code  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys! here is the code :


supposed output is :

Morgan Freeman
Brad Pitt
Kevin Spacey

Question : how i can repair my mistakes? Thanks!
 
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the program just run and not do anything?
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect you are encountering a NullPointerException as the instance variable s (in Student class) is null.

However, you don't need the variable s. Your printFullName() method should be changed to output firstName and lastName to System.out. I will leave that for you to change.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please TellTheDetails (<-- click that). We don't know what the problem is unless you tell us.
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code throws "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, insert "}" to complete Block at goodday.Main.main(Student.java:26)" . Before ecexuting, the code highlijghts 2 lines: System.out.println(s.printFullName()); and public class Main {.
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Your printFullName() methot is of type void so you can't pass it to the System.out.println().
If you wan't to make a method that prints Student name just put firstName and lastName to System.out.println() in it.
Why your Student class has an instance variable Student?
An instance variable means "has a". So why your Student has a Student?
And that other Student should have its own and so on and so on (until one of them has null or this ...)

I think you use some IDE do learn Java. Don't do that. IDE makes lots of work instead of you.
It is nice to work with Java but it is not a good idea to use it for learning Java.
When something goes wrong you suddenly don't understand why...
Just use Notepad++ and compile from command line. It's fun .
 
Dana Horst
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you wan't to make a method that prints Student name just put firstName and lastName to System.out.println() in it. - it works, indeed thank you
Why your Student class has an instance variable Student?
An instance variable means "has a". So why your Student has a Student? - i use already created code, just need to add some method to let the full name display. variable Student was later used in a for loop, but i am not actually.


I think you use some IDE do learn Java. Don't do that. IDE makes lots of work instead of you.
It is nice to work with Java but it is not a good idea to use it for learning Java.
When something goes wrong you suddenly don't understand why...
Just use Notepad++ and compile from command line. It's fun . - yes you are right, i use Eclipse. well, notepad++ just downloaded, thanks for a tip! and thanks for reply !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!