Toymods Car Club
www.toymods.org.au
F.A.Q. F.A.Q.    Register Register    Login Login    Home Home
Members Members    Search Search
Toymods » General Car Talk » hey java buffs

Show: Today's Posts  :: Show Polls 
Email to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
AuthorTopic
RA28
Forums Junkie


Registered:
May 2002
 
hey java buffs Mon, 05 May 2003 05:56 Go to next message
didn't post this on a java forum cause all the nerds would laugh me sorry for clogging up toymods with it but someone out there should be able to help me.

I wanna creat an array of objects, as stored in a class file.

class file is called bullet.class

I use the line

bullet b[] = new bullet[10]

and it seems to create the objects fine but when you try to access them it gives me a nullpointerexception error.

I realise that an object created in an array is given a null value but how do I initialise them??

thanks, tim.
  Send a private message to this user    
Cory
Regular


Location:
Melbourne
Registered:
June 2002
 
Re: hey java buffs Mon, 05 May 2003 06:23 Go to previous messageGo to next message
either do

bullet b[] = {0,0,0,0,0,0,0,0,0,0};

or put this in the constructor:

for (int i = 0; i < b.size(); i++)
{
b[i] = 0;
}
  Send a private message to this user    
Nark
Forums Junkie


Location:
Cabramatta, NSW
Registered:
May 2002
      Nark@toymods.net/Work
icon3.gif  Re: hey java buffs Mon, 05 May 2003 07:31 Go to previous messageGo to next message
And additionally, around any code that accesses the array, do a

if (b[blah] != null)
{

}


Either that or put it in a try loop with a catch for NullPointerExceptions.

Extra security 'coz you can't always ensure that your data is clean.

[Updated on: Mon, 05 May 2003 07:35]

  Send a private message to this user    
RA28
Forums Junkie


Registered:
May 2002
 
Re: hey java buffs Mon, 05 May 2003 11:14 Go to previous messageGo to next message
tnanx guys will give it a go, only just sinking my teeth into java, only ever been a hobbiest VB programmer b4.

will let you know.
  Send a private message to this user    
Nark
Forums Junkie


Location:
Cabramatta, NSW
Registered:
May 2002
      Nark@toymods.net/Work
Re: hey java buffs Tue, 06 May 2003 03:02 Go to previous messageGo to next message
VB?! Eww!!!

BASIC should've died when the Commodore 64 died! Very Happy

10 PRINT "Max r00lz and BASIC sux0rz"
20 GOTO 10

hehe
  Send a private message to this user    
ehendrikd
Forums Junkie


Location:
ballarat
Registered:
April 2003
 
Re: hey java buffs Tue, 06 May 2003 03:40 Go to previous messageGo to next message
bullet b[] = new bullet[10];

for (int i = 0; i < b.length; i++)
b[i] = new bullet();//or whatever you initialise them with
  Send a private message to this user    
SupraPete
Forums Junkie


Location:
Sydney
Registered:
July 2002
   
Re: hey java buffs Tue, 06 May 2003 03:48 Go to previous messageGo to next message
yep ehendrikd is right.

If you try and initialise them the way that cory says then you'll get a ClassCastException - Can't cast Bullet as an int (zero).

Putting it in the constructor won't work either (which constructor?!?)

Pretty much you'll need to put in ehendrikd's code BEFORE you try and access it.


Testing for null pointers like Nark says is a good safety measure. But no one puts stuff in a try block (not a loop) specifically for NullPointerException(s) though, just for the base Excepton and then print the stacktrace to see what the exception is.


Sorry to be anal, but the compiler will pick it up. And doing a try for a null pointer is just bad practise. I won't let any of my team at work do those kind of things (they are quite resourseful in their different types of bad programming practises techniques)
  Send a private message to this user    
SupraPete
Forums Junkie


Location:
Sydney
Registered:
July 2002
   
Re: hey java buffs Tue, 06 May 2003 03:52 Go to previous messageGo to next message
oh and the first line will need capital B's unless you actually setup your class with a lower case b (bad practise too!!)



Bullet b[] = new Bullet[10];

for (int i = 0; i < b.length; i++)
b[i] = new Bullet();//or whatever you initialise them with

  Send a private message to this user    
ehendrikd
Forums Junkie


Location:
ballarat
Registered:
April 2003
 
Re: hey java buffs Tue, 06 May 2003 03:57 Go to previous messageGo to next message
HKSPete wrote on Tue, 06 May 2003 13:48


Sorry to be anal, but the compiler will pick it up.


compiler == anal
  Send a private message to this user    
Nark
Forums Junkie


Location:
Cabramatta, NSW
Registered:
May 2002
      Nark@toymods.net/Work
Re: hey java buffs Tue, 06 May 2003 05:51 Go to previous messageGo to next message
HKSPete wrote on Tue, 06 May 2003 13:48

Testing for null pointers like Nark says is a good safety measure. But no one puts stuff in a try block (not a loop) specifically for NullPointerException(s) though, just for the base Excepton and then print the stacktrace to see what the exception is.

Sorry to be anal, but the compiler will pick it up. And doing a try for a null pointer is just bad practise. I won't let any of my team at work do those kind of things (they are quite resourseful in their different types of bad programming practises techniques)


Depends on how everything is set up. Stuff like null pointers shouldn't create a stack trace if it's fixable.
I wouldn't put a try loop JUST to catch a NullPointerException, I'd just put the if blocks around it.
But sometimes you can fix it elegantly with a catch for null pointers. Like verify that the rest of the data is clean or even throw more exceptions for them to casade outward.
Really depends on your architecture.
  Send a private message to this user    
Cory
Regular


Location:
Melbourne
Registered:
June 2002
 
Re: hey java buffs Tue, 06 May 2003 07:01 Go to previous messageGo to next message
HKSPete wrote on Tue, 06 May 2003 13:48


If you try and initialise them the way that cory says then you'll get a ClassCastException - Can't cast Bullet as an int (zero).




errr...whoops Embarassed wasnt paying attention there...

i was meant to write what ehendrikd has but i think i got sidetracked somewhere Rolling Eyes
  Send a private message to this user    
SupraPete
Forums Junkie


Location:
Sydney
Registered:
July 2002
   
Re: hey java buffs Tue, 06 May 2003 07:46 Go to previous messageGo to next message
Nark wrote on Tue, 06 May 2003 15:51

Depends on how everything is set up. Stuff like null pointers shouldn't create a stack trace if it's fixable.
I wouldn't put a try loop JUST to catch a NullPointerException, I'd just put the if blocks around it.
But sometimes you can fix it elegantly with a catch for null pointers. Like verify that the rest of the data is clean or even throw more exceptions for them to casade outward.
Really depends on your architecture.


Sorry I was a bit vage. I meant your IF statement was the right way to go (you are right, I'm not dissagreeing with you), but a specific try BLOCK (its a block not a bloody "loop"!! Mad ) for just a null pointer is silly. Which is exactly what you said in the quote.

As for elegant catches YUK. No such thing. A catch is ONLY if there is something that wasn't expected. If there is a possibility there is a null, test for it, don't expect the try block to pick it up. Putting more than e.printStackTrace() or throwing another exception within a catch block is disgusting code.


I didn't know there were this many java learners/programmers in Toymods Shocked Shocked . Just remember grasshopper(s):

Computers are a great way to make money. Money that can be spent on doing up toyotas!
  Send a private message to this user    
justcallmefrank
Forums Junkie


I supported Toymods

Location:
Perth
Registered:
May 2002
 
Re: hey java buffs Tue, 06 May 2003 08:05 Go to previous messageGo to next message
Thats the hope I have by going into bloody computers Razz
  Send a private message to this user    
SupraPete
Forums Junkie


Location:
Sydney
Registered:
July 2002
   
Re: hey java buffs Tue, 06 May 2003 08:11 Go to previous messageGo to next message
One day you could own a 1UZ too Very Happy Very Happy Very Happy
  Send a private message to this user    
justcallmefrank
Forums Junkie


I supported Toymods

Location:
Perth
Registered:
May 2002
 
Re: hey java buffs Tue, 06 May 2003 08:12 Go to previous messageGo to next message
Ney, one day I will own a 3UZ Nod
  Send a private message to this user    
Ribbo
Forums Junkie


I supported Toymods

Location:
Northern Beaches
Registered:
May 2002
 
Re: hey java buffs Tue, 06 May 2003 08:16 Go to previous messageGo to next message
hehe im in my final year of comp sci at syd uni.... so many of us.. java, takes all the fun out of programming hehe
where are all the core dumps from memory managment from C and C++ hehe
  Send a private message to this user    
justcallmefrank
Forums Junkie


I supported Toymods

Location:
Perth
Registered:
May 2002
 
Re: hey java buffs Tue, 06 May 2003 08:19 Go to previous messageGo to next message
Haha, third year Software Engineering at Curtin Uni Perth, 1 and 3/4 years to go Razz Thats if I don't make it longer again!
  Send a private message to this user    
Nark
Forums Junkie


Location:
Cabramatta, NSW
Registered:
May 2002
      Nark@toymods.net/Work
Re: hey java buffs Tue, 06 May 2003 08:55 Go to previous messageGo to next message
HKSPete wrote on Tue, 06 May 2003 17:46

Sorry I was a bit vage. I meant your IF statement was the right way to go (you are right, I'm not dissagreeing with you), but a specific try BLOCK (its a block not a bloody "loop"!! Mad ) for just a null pointer is silly. Which is exactly what you said in the quote.

As for elegant catches YUK. No such thing. A catch is ONLY if there is something that wasn't expected. If there is a possibility there is a null, test for it, don't expect the try block to pick it up. Putting more than e.printStackTrace() or throwing another exception within a catch block is disgusting code.


I didn't know there were this many java learners/programmers in Toymods Shocked Shocked . Just remember grasshopper(s):

Computers are a great way to make money. Money that can be spent on doing up toyotas!


Block, loop, you know what I meant. Razz
I have to sneak my foruming in between work (got management snooping around) so most of my replies aren't well thought out. Smile

I didn't say elegant catches, I meant elegant handling of thrown exceptions.
I disagree with you on the stack traces. They are evil. If you can handle an exception and can fix the problem, then do so at all costs. Stack traces are only useful if you're debugging.
And, no I don't catch NullPointerExceptions, I use the if/else blocks, but I'm not talking about null pointers specifically.
  Send a private message to this user    
SupraPete
Forums Junkie


Location:
Sydney
Registered:
July 2002
   
Re: hey java buffs Tue, 06 May 2003 09:59 Go to previous message
Nark wrote on Tue, 06 May 2003 18:55

I didn't say elegant catches, I meant elegant handling of thrown exceptions.
I disagree with you on the stack traces. They are evil. If you can handle an exception and can fix the problem, then do so at all costs. Stack traces are only useful if you're debugging.



Definately stack traces are only good for debugging, I always leave them in when the code goes to production as if there ever is an error that wasn't accounted for then you know where to look. Only way to do this properly is to use a good logger and log all the exceptions properly. Log4j is pretty good for this kind of stuff.
  Send a private message to this user    
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic:Sydney autosalon
Next Topic:broken again
Goto Forum:
-=] Back to Top [=-

Current Time: Sat May 18 14:01:57 UTC 2024

Total time taken to generate the page: 0.0058751106262207 seconds

Bandwidth utilization bar

.:: Contact :: Home ::.

Powered by: FUDforum 2.3.8
Copyright ©2001-2003 Advanced Internet Designs Inc.