Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

error in a method in my class  RSS feed

 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello very good morning, I have the following problem when compiling a .main apparently I do something wrong in a class and call the method
from the .main it gives me error.

But i do not know why...

the class is this.

And the method that fails me is the last

I call the method from the .main follows

Giving me an error and not the netbeans I missing?

A builder ??

 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please tell us what kind of error you're getting (copy/paste).

Another thing. Your construct has parameter 'Date d' which isn't used.
When declaring arrays, try to place '[]' right after the data type instead of variable name (it is common convention, so more readable).
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch.

Please tell us what kind of error you're getting (copy/paste).

Another thing. Your construct has parameter 'Date d' which isn't used.
When declaring arrays, try to place '[]' right after the data type instead of variable name (it is common convention, so more readable).


hello very good morning and thanks for answering.

Explain what the parameter Date, really is that pryecto has several classes, I'll put the problem and then perhaps look more
clear because I do not see the truth ..


I have five classes plus the .main.

Classes are siguients

Person
--------


Score
-----

Player
-------



Team
-----



Match
------

The .main have to take this
-------------------------------------


Well I have two problems.

Within the class .match the method void addScore gives me error and do not understand why and method getScore Void (Team t)
I've been thinking two whole days but can not find what the problem is truth.

I hope you can help me or guide me with the solution.

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Please tell us what kind of error you're getting (copy/paste).


Highly agree. It would help if you tell us what the error is. Saying that you got an error, in a certain class, and how long you have been working on it, doesn't tell us what the compiler is complaining about.

Henry
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Liutauras Vilda wrote:
Please tell us what kind of error you're getting (copy/paste).


Highly agree. It would help if you tell us what the error is. Saying that you got an error, in a certain class, and how long you have been working on it, doesn't tell us what the compiler is complaining about.

Henry



Sorry, You are right the truth.

I pass the compiler errors that sent me.

Exception in thread "main" java.lang.NullPointerException
at javaapplication2.Match.addScore(Match.java:46)
at javaapplication2.JavaApplication2.main(JavaApplication2.java:76)

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the error message tells you exactly where the exception happens: in line 46 of Match.java. That line looks like this:

You get a NullPointerException there because the variable scores is null.

scores is a member variable of class Match, that you declared in line 23 of Match.java. But you never initialized this member variable, so it is null. You have to assign a value to it somewhere, for example in the constructor of class Match. Something like:
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't seem to be initialized Scores array in your Match class, which is on Line 23. So trying to add an element to a null reference.
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that the error message tells you exactly where the exception happens: in line 46 of Match.java. That line looks like this:

You get a NullPointerException there because the variable scores is null.

scores is a member variable of class Match, that you declared in line 23 of Match.java. But you never initialized this member variable, so it is null. You have to assign a value to it somewhere, for example in the constructor of class Match. Something like:


Perfect! My God ... I did not realize that ... I had to initialize the array 6 is the time to call the function ...

Thank you very much!!

Now catch me all results ...

Now the method fails me

Void public getScore (Team t) {
       
            
    }


The compiler gives me these errors, I should write something in the void but certainly not put ....

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at javaapplication2.Match.getScore (Match.java:78)
at javaapplication2.JavaApplication2.main (JavaApplication2.java:85)

Thank you very much for the help you are giving me.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who wrote this code?
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Who wrote this code?


The most code is mine, my teacher gave us the .main and classes in a UML diagram that we had to move to java and make it work with the .main.

We commented that the most difficult was this method

Void public getScore (Team t) {
    }

And I can only this method to implement.

I gave a lot of laps and probe things but really do not understand how
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at javaapplication2.Match.getScore (Match.java:78)

Note: Void is the name of a class, void is a keyword indicating the method does not return a value.
With Void the compiler expects the method to return an instance of the Void class.
Java is case sensitive.  You probably meant to use void for the method definition.
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at javaapplication2.Match.getScore (Match.java:78)

Note: Void is the name of a class, void is a keyword indicating the method does not return a value.
With Void the compiler expects the method to return an instance of the Void class.
Java is case sensitive.  You probably meant to use void for the method definition.


ok, you're absolutely right it was void in capital letters, it is now corrected dal mistake and I do not understand.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at javaapplication2.JavaApplication2.main (JavaApplication2.java:85)

Are the lines 85 and 86 of the main that call getScore

System.out.println ( "result Match 1 team 1:" + m1.getScore (t1));
System.out.println ( "result Match 1 team 2:" + m1.getScore (t2));


No I have to put in the void method to treat the parameter
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Rodriguez Barcelo wrote:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at javaapplication2.JavaApplication2.main (JavaApplication2.java:85)


First read this...

https://coderanch.com/wiki/660183/Wiki/Fix-Compiler-Errors-Running-Application

Trying to fix compiler errors, only after finding them in runtime, is really not a good idea. See Wiki post for reason why.

Guillermo Rodriguez Barcelo wrote:
System.out.println ( "result Match 1 team 1:" + m1.getScore (t1));
System.out.println ( "result Match 1 team 2:" + m1.getScore (t2));


No I have to put in the void method to treat the parameter


Methods with void return types return nothing. That is what void means. This means that there is nothing to use -- hence, nothing to concatenate with the string in order to print it.

Henry
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Guillermo Rodriguez Barcelo wrote:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: <any>
at javaapplication2.JavaApplication2.main (JavaApplication2.java:85)


First read this...

https://coderanch.com/wiki/660183/Wiki/Fix-Compiler-Errors-Running-Application

Trying to fix compiler errors, only after finding them in runtime, is really not a good idea. See Wiki post for reason why.

Guillermo Rodriguez Barcelo wrote:
System.out.println ( "result Match 1 team 1:" + m1.getScore (t1));
System.out.println ( "result Match 1 team 2:" + m1.getScore (t2));


No I have to put in the void method to treat the parameter


Methods with void return types return nothing. That is what void means. This means that there is nothing to use -- hence, nothing to concatenate with the string in order to print it.

Henry


ok, I understand but then as I do the main work?
Change the lines?
System.out.println ( "result Match 1 team 1:" + m1.getScore (t1));
System.out.println ( "result Match 1 team 2:" + m1.getScore (t2));


I remove that method?

It's not that I do to make it work ... It is clear that if I call a void method returns nothing but me then?

Thanks for answering!
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't look that there should be void. You should be returning something most likely as you want to use those in a print statement.

Why you chose there to use 'void' as a return type (which is no return) ?
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:It doesn't look that there should be void. You should be returning something most likely as you want to use those in a print statement.

Why you chose there to use 'void' as a return type (which is no return) ?


What I want is to use a void method that returns me a string without changing the type of void to String ....

Just I try changing the type of meotodo a String and if I work so ....

public String getScore (Team t)
    {
         String result = "";
        
         ///////////
         result = t.getCity ();
        
         return result;
}

But what I want is to be "void" method ....

Since I ask you not Cambiel type method ..

I never thought of using a global variable and call the method ...

But not how to implement it.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Rodriguez Barcelo wrote:
What I want is to use a void method that returns me a string without changing the type of void to String ....


Not possible. Either a method returns a value, or it doesn't return a value. It can't do both.

Henry
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Rodriguez Barcelo wrote:What I want is to use a void method that returns me a string without changing the type of void to String ....
That is impossible in Java.

What you can do with void, is to print something out and do some other routine which doesn't return anything as a value.

An example of printing something out:

An example of doing other routine:

But that is pretty much it in terms of idea what you can do with a method which has 'void' return type.

I'm having hard times to understand your reasoning why you want to do one way but not the other.

Do you have any written requirements for your project, so you could show us?
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If what you want is a 'void' method that 'returns a value', then you have these 2 concepts in conflict with each other. You can have the return keyword in a void method, but it will never be more than 'return;' -- as in, it returns nothing.

A method which returns as simply as yours does is known as an Accessor. A method defined with type void will never return a value, it is usually a method to mutate some value from within a class (although not always the case).

If you want to return the value you're asking for, you need to be returning a String, or an int, which will be accepted by the println() method. You can have a void method that isn't used as a value within println(), such as:



Which is similar to the example you've provided in your last answer, without the println() method.

I hope this helps,

L.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on a cow when you have only been here such a short time
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Is there a way I can find who gave it?
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Guillermo Rodriguez Barcelo wrote:What I want is to use a void method that returns me a string without changing the type of void to String ....
That is impossible in Java.

What you can do with void, is to print something out and do some other routine which doesn't return anything as a value.

An example of printing something out:

An example of doing other routine:

But that is pretty much it in terms of idea what you can do with a method which has 'void' return type.

I'm having hard times to understand your reasoning why you want to do one way but not the other.

Do you have any written requirements for your project, so you could show us?


What they ask me is to compile the .main.

Right now I have everything working except the last lines of .main

// Results
        System.out.println ( "result Match 1 team 1:" + m1.getScore (t1));
        System.out.println ( "result Match 1 team 2:" + m1.getScore (t2));
        System.out.println ( "Winner Match 1:". + M1.getWinner () getName ());
        System.out.println ( "result match" + m1.getScores ());
        System.out.println ( "result match" + m2.getScores ());
        System.out.println ( "winner match 2:" + m2.getWinner () getName ().);


The blame for this is the method we are talking about the:

public void getScore (Team t)


which is then called by
getScores public String () {}

And I really do not know how to do to fill the void method because they will not let me use another, I use that kind, why I'm so busy.

Sorry to be so heavy, keep thinking about the subject but not sack.



 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Rodriguez Barcelo wrote:
The blame for this is the method we are talking about the:

public void getScore (Team t)

which is then called by
getScores public String () {}

And I really do not know how to do to fill the void method because they will not let me use another, I use that kind, why I'm so busy.


Don't know what more to add here. The Caller and Callee are incompatible. You either have to change the main class, or the match class. And if you can't change either, then, you hands are pretty much tied.

Henry
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Guillermo Rodriguez Barcelo wrote:
The blame for this is the method we are talking about the:

public void getScore (Team t)

which is then called by
getScores public String () {}

And I really do not know how to do to fill the void method because they will not let me use another, I use that kind, why I'm so busy.


Don't know what more to add here. The Caller and Callee are incompatible. You either have to change the main class, or the match class. And if you can't change either, then, you hands are pretty much tied.

Henry


Well that method fails indicating that I change the type and I cambiare For a string type explaining it ...

Finally tell you that in 82 of the main lina. when you

System.out.println ( "Winner Match 1:". + M1.getWinner () getName ());

I get an error Exception in thread "main" java.lang.NullPointerException

But that mistake gives me the method
Team getWinner public () {
     return null;
What I have to return null ..

I put that there? I understand that should return the name of the winning team in the Team class but do not have that method ....
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Rodriguez Barcelo wrote:What they ask me is to compile the .main.

Right now I have everything working except the last lines of .main

So you have been given class JavaApplication2 by your instructor to mimic test cases.

By looking to that class's print statements:
Guillermo Rodriguez Barcelo wrote:

I can say, that non of the methods which are used within the print statements should have 'void' return type. Havind said that, it means, each of them need to return some value.

I'll repeat my question again
Earlier today I wrote:Why you chose there to use 'void' as a return type ( in the method getScore() ) ?
 
Guillermo Rodriguez Barcelo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Guillermo Rodriguez Barcelo wrote:What they ask me is to compile the .main.

Right now I have everything working except the last lines of .main

So you have been given class JavaApplication2 by your instructor to mimic test cases.

By looking to that class's print statements:
Guillermo Rodriguez Barcelo wrote:

I can say, that non of the methods which are used within the print statements should have 'void' return type. Havind said that, it means, each of them need to return some value.

I'll repeat my question again
Earlier today I wrote:Why you chose there to use 'void' as a return type ( in the method getScore() ) ?


As you have all the reason, what I do is change the type void by the necessary and explain it in my exercise.

Then as quedaria the method to get the scores?

I've been a mess in the head lol amazing, thank goodness you help me
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it is marked assignment probably, I don't think it merrits a good mark as it is now, so keep in mind that it requires a lot of improvement before you submit. It sounds from your side that you only failing on last print statement within your main tester class - that is not true!

There are at least couple of not implemented methods. One method's implementation is "return null", which is wrong.

I'm a bit surprised how you managed to write such an amount of code, but can't crack out such a similar thing. Not sure how else I could help you. You might better revise some slides from your class so you could remind yourself about 'void'.

Simple thing, I'll repeat once again and as many others mentioned to you already, in print statement there is used method which doesn't return any value while it is expected to do so. Read posts once again to remind yourself which one.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exercise is fairly simple, it doesn't seem to me that it requires a lot logical thinking, reasoning in order to solve it. You have been given UML diagram pressumably with method signatures. So what you need is - a bit of discipline and implement one method at a time and test it if it works as expected and then move again.

What I'd do if I were you

Would comment out most of the code in your main/tester class given by your instructor and leave only 1 test case. Then implement relevan method and see if the output makes sense/correct.
Then uncomment out another print statement and implement until no compilation errors appear and run code again to see if the output is correct. Then do the same with other methods.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you solving this exercise now doesn't seem it will go well. Sorry for being honest. Such assignments are a good chance to get 100%, which is max marks from the practical assignment so you'd have less stress during the exam. You don't need to improvise - you just need to do what is provided to you already.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have time and willing to start over?
By implementing one class at a time, where within each class you'd implement one method at a time by testing each of them before you move further.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Hasan wrote:Thank you!

You're most welcome. It was well deserved. Great post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!