d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Easy Java Quistion. > Null Pointer Exception
Add Reply New Topic New Poll
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Aug 9 2012 02:02pm
Have an array that holds 10 doubles (called doubles here).
At some point it need to be reset so all values equals zero.

If I just do doubles = null I get an null pointer exception.

I can do a for loop

for (int n = 0; n<10; n++)
doubles[n]=0;

But istnt there an easier and faster way ?

thanks :)
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Aug 9 2012 02:16pm
easier and faster? this is already the fastest and easier way...
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Aug 9 2012 02:38pm
Quote (tigeranden @ Aug 9 2012 03:02pm)
Have an array that holds 10 doubles (called doubles here).
At some point it need to be reset so all values equals zero.

If I just do doubles = null I get an null pointer exception.

I can do a for loop

for (int n = 0; n<10; n++)
doubles[n]=0;

But istnt there an easier and faster way ?

thanks :)


If you had a list of 10 bajillion elements I would worry about the program being fast or not.

A simple for loop is just fine for 10 elements.
Member
Posts: 61,418
Joined: Nov 21 2006
Gold: Locked
Aug 9 2012 02:42pm
can't you just do
Code
doubles = new double[10];

the default value for a double in an array would be 0.0 anyway

This post was edited by Ectasy on Aug 9 2012 02:44pm
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Aug 9 2012 02:42pm
ty both :)
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Aug 9 2012 02:48pm
Quote (Ectasy @ Aug 9 2012 09:42pm)
can't you just do
Code
doubles = new double[10];

the default value for a double in an array would be 0.0 anyway



Isnt that a bad practice as it "hides a field". ?
Doubles is an Instance variable declared in the head of the class.

This post was edited by tigeranden on Aug 9 2012 02:49pm
Member
Posts: 61,418
Joined: Nov 21 2006
Gold: Locked
Aug 9 2012 02:50pm
Quote (tigeranden @ 9 Aug 2012 15:48)
Isnt that a bad practice as it "hides a field". ?
Doubles is an Instance variable declared in the head of the class.


what do you mean by hides a field?
at any rate, if you don't want to use what i posted for whatever reason - just use what you had originally. it's not going to really matter either way unless you plan on doubles having an extremely large length
Member
Posts: 4,478
Joined: Feb 10 2010
Gold: 1,806.00
Aug 9 2012 02:54pm

Netbeans gives the warning "hides a field" if I overwrite an instance variable in a method body.

Just trying to understand something. I value your input.
Member
Posts: 29,197
Joined: Feb 5 2007
Gold: 4,000.18
Aug 9 2012 02:57pm
Quote (tigeranden @ Aug 9 2012 03:54pm)
Netbeans gives the warning "hides a field" if I overwrite an instance variable in a method body.

Just trying to understand something. I value your input.


Hiding fields usually relates to inheritance, but making a whole new list is overkill when a simple for loop can do the job.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 9 2012 05:44pm
Quote (IsraeliSoldier @ Aug 9 2012 04:57pm)
Hiding fields usually relates to inheritance, but making a whole new list is overkill when a simple for loop can do the job.


Quote (tigeranden @ Aug 9 2012 04:48pm)
Isnt that a bad practice as it "hides a field". ?
Doubles is an Instance variable declared in the head of the class.


this probably has nothing to do with inheritance.

i'm guessing tiger's code is

Code
double[] doubles;

somemethod()
{
 double[] doubles = new double[10]
}


in which case he's declaring the variable twice. remove one of the declarations

This post was edited by carteblanche on Aug 9 2012 05:44pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll