• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

no exeception is raised  RSS feed

 
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a program of one class with 6 methods and a program hand in my class with keyboard, it is a candidate management program at a large French school ...

I added to each of the methods the corresponding "try catch" to trace my coding error.

My program compiles and executes but I have an error of "java.lang.NullPointerException" and moreover no exeception is raised from where does it come from?
 
Saloon Keeper
Posts: 2576
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:... I have an error of "java.lang.NullPointerException" and moreover no exeception is raised from where does it come from?


Can you share the stack trace for the exception, and the associated code that it points-to?
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Philippe Ponceblanc wrote:... I have an error of "java.lang.NullPointerException" and moreover no exeception is raised from where does it come from?


Can you share the stack trace for the exception, and the associated code that it points-to?



my code will seem cabalistic because it is written in French, I'm sorry




> run DossierCandidatureGER
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
>  

 
Ron McLeod
Saloon Keeper
Posts: 2576
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the exception is being thrown from some kind of container (edu.rice.cs.drjava.model.compiler.JavacCompiler) which is used to compile-and-launch the application.  Most likely the main method should probably be static
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:It looks like the exception is being thrown from some kind of container (edu.rice.cs.drjava.model.compiler.JavacCompiler) which is used to compile-and-launch the application.  Most likely the main method should probably be static



if the main method is "static" I have two new error that appears when compiling the source code.


2 errors found:
File: /home/phipo/fun-mooc-java-obj/mooc_2ieme_session/DossierCandidatureGER.java  [line: 131]
Error: non-static variable Epreuve_Ecrite cannot be referenced from a static context
File: /home/phipo/fun-mooc-java-obj/mooc_2ieme_session/DossierCandidatureGER.java  [line: 134]
Error: non-static variable Epreuve_Orale cannot be referenced from a static context

 
Ron McLeod
Saloon Keeper
Posts: 2576
323
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:if the main method is "static" I have two new error that appears when compiling the source code.


The problem is that you are trying to assign instance variables as if they were class variables.

Without restucturing your code, you could either make them class variables by making them static:

Or assign the values to the instance after creating it:
 
Sheriff
Posts: 6747
466
BSD Linux Mac OS X VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philippe Ponceblanc, almost all these try-catch blocks are redundant as the exceptions couldn't occur there.

By simply doing something similar as you do, for example: boolean isAlive = true; can't throw an exception, that's technically impossible, and all your cases are very similar to that example I just showed.

However, I would worry least about the exception or why it is thrown or not. I believe that program needs to start from disciplined thoughts, which seemed to be lacking in your approach. Program is hard to understand, as well as isn't clear its intention.

Do you have some sort of requirement, for instance, what kind of feature(s) you need to implement, so we could discuss it maybe in a more concrete manner? I'm surprised you did not lose patience writting so much code with quite an uncertainty.

When I say uncertainty, I mean code snippets like the one showed below:

What are your intentions with it?
 
Bartender
Posts: 704
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main() method must be declared static. If it is not, then that method is an instance method and there is no entry point for your program.

You must create an instance of DossierCandidatureGER before you try to access any of the instance variables, and you have to reference them via that instance variable:

As others have said, get rid of all those extraneous try...catch blocks.

Why do you keep creating more Scanner instances? One is enough!
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod Thank you for your lesson on the static method and its member variables. my program works.

Liutauras Vilda, I understand that my code is not very clear, it comes from the code of methods is imposed by the certification that I am doing,
there is an automatic corrector, I too there are things that I escape, I think I'll understand later ....

However, the worst is that my toString method is very poorly written, I lack imagination to write the latter properly.




how can I see the writing of the code in another aspect ?
 
Marshal
Posts: 5988
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is a good example of why you should not overuse try/catch blocks.  There's no reason for it.  Secondly, format your code correctly.  Without the proper formatting, it's hard to see the structure and logic of the code.  See HowToFormatCode (that's a link).  

So your code should first look something like this:
Now it's easy to see the logic of the code. So do you want all that logic in the else clauses to execute only if the if statement is true?  If so, then this is probably fine as a toString().  
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
apparently my static variables:
 static int Epreuve_Ecrite;
 static int  Epreuve_Orale;
are not taken into account in my code.




If I try to display the value of one of them, the notes are empty.



Epreuve_Ecrite is empty !
Certainly a static variable problem?
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:. . .
apparently my static variables:
 static int Epreuve_Ecrite;
 static int  Epreuve_Orale;
are not taken into account in my code.
. . .

Why did you make them static? I usually think that having something static without a good reason is a serious mistake. I don't believe those variables should be static in the first place.
I also think they should have been written épreuveOrale and  épreuveEcrite. Look at the old Sun Style Guide. I shall let you work out what the mistake in that link is. If you write variable names Epreuve_Ecrite and Epreuve_Ec in different places, you are risking confusing yourself about which is which.
 
Philippe Ponceblanc
Ranch Hand
Posts: 210
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Philippe Ponceblanc wrote:if the main method is "static" I have two new error that appears when compiling the source code.


The problem is that you are trying to assign instance variables as if they were class variables.

Without restucturing your code, you could either make them class variables by making them static:

Or assign the values to the instance after creating it:


I am advised to write with static variables if no I have errors on my variable, it seems to me that you are not all agree on this subject.
I do not understand why you are not in corelation.

 
Campbell Ritchie
Marshal
Posts: 64494
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Philippe Ponceblanc wrote:. . .I am advised to write with static variables . . .

That “advice” is incorrect. Who told you that?

I do not understand why you are not in corelation.

A static variable doesn't constitute part of the object. It belongs to the class. There is only one copy of each static member of a class; it has the same value for all instances.
Create one examen instance and then a second with different marks. Go back to the first instance and see what the values of the two static fields are.
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!