Null Pointer Exception and scope in Java?

Here you're declaring a local variable Ratings and assigning x to it. Right afterwards, the variable goes out of scope.

Public Rating (double x){ double Ratings = x; } Here you're declaring a local variable Ratings and assigning x to it. Right afterwards, the variable goes out of scope. What you want to do is Ratings = x;, which would set the instance variable Ratings to x instead of creating a new and unused local variable.

Here is your code, nicely formatted and the bug fixed: package com.yourdomain.advisor. Score; public class Rating{ double ratings; double sum; double rawAdvisor; double advisorScore; public Rating(final double x){ this. Ratings = x; } public double score(){ for(int I = 2; I Length; i++){ this.

Sum += this. Ratingsi; } this. RawAdvisor = (this.

Sum - 3 * (this.ratings. Length - 2)) / 4; this. AdvisorScore = 2.5 + 2.5 * (1 - Math.

Pow(Math. E, -.5 * this. RawAdvisor)); return this.

AdvisorScore; } public void print(){ System.out. Println(this. AdvisorScore); } public static void main(final String args){ final double p1_1 = { 101, 1, 1, 1.5, .5 }; final Rating d = new Rating(p1_1); d.score(); d.print(); } } You were assigning a value to an unused local variable instead of a field.

And I added a few changes according to java conventions: package names are lower case letters and periods only and should reflect your domain name class names are in TitleCase method, field and local variable names are in camelCase See the Sun Java Naming Conventions Also, do yourself a favor: use an IDE like Eclipse and turn code formatting on.

1 The return value of score is still just discarded though when called as d.score() in main. Looks like it should really be void – barrowc Sep 15 '10 at 1:03 Yeah I know, didn't touch that. One option would be to return Rating and make it a fluent interface.

– Sean Patrick Floyd Sep 15 '10 at 4:34.

Because your Ratings which is member of your class is not initialized just declared so it is currently referring to NULL. And you can't execute method on NULL in constructor you should use this. Ratings = x; which will initilize your member variable which you are trying to use in score method.

I would suggest you to go through tutorials.

The problem, is that you are defing a new variable name Ratings intead of using the one you already have defined, so Rating is not defined when calling score. In your constructor, you shouldn't redefine it, just use it. Public Rating (double x){ Ratings = x; } I think this should work.

You got two variables with the same name Change public Rating (double x){ double Ratings = x; } to public Rating (double x){ Ratings = x; }.

Needs to be initialized or be set some non null value.

The problem arises from trying to reference the Ratings instance variable defined at the top of the class. This is never assigned to because within your constructor you are creating a local variable Ratings and assigning x to that. The local variable is then immediately discarded leaving the Ratings instance variable pointing to null.

The fix is simple: public Rating (double x){ this. Ratings = x; }.

In your constructor, you are assigning the array to a local variable rather than the Ratings field: public Rating (double x){ double Ratings = x; } should be public Rating (double x){ Ratings = x; } or even, if you want to be more explicit public Rating (double x){ this. Ratings = x; } I would also advise capitalising Ratings in this way, as it is against Java conventions which makes it confusing to read; google for more information on the topic of naming conventions.

Your constructor declares a new variable Ratings that hides the instance variable. Package names should begin with lower case letters. Variable names should begin with lower case letters.

I think in constructor you meant to initialize the class member public double Ratings,but in constructor you are creating another double Ratings,which is local to constructor. Change the constructor to public Rating (double x){ double Ratings = x; } It will work.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions