For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2007 > NullPointerException!!!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author NullPointerException!!!
jack90830@gmail.com

2007-11-13, 10:14 pm

//template for checking combinations of card

import javax.swing.*;
public class template {
public static void main(String[] args)
{
Card handCard[] = new Card[5];
for(int i=0;i<5;i++)
{
handCard[i].num = 1 + (int)(Math.random( ) * 13);
handCard[i].suit = 1 + (int)(Math.random( ) * 4);
System.out.println(handCard[i].num);
System.out.println(handCard[i].suit);
}
System.exit(0);
}

public static class Card
{
Card(int n, int s)
{
n = 0;
s = 0;
}
public int num = 0;
public int suit =0;
}
}
==============================
> Executing: C:\Program Files\ConTEXT\ConExec.exe "C:\Program Files\Java\j2sdk1.4.2_16\bin\java.exe" template


java.lang.NullPointerException
at template.main(template.java:10)
Exception in thread "main"
> Execution finished.

===============================

-----------------------------------------------------------
//from sun's website
Thrown when an application attempts to use null in a case where an
object is required. These include:
Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.
----------------------------------------------------------------------
i found those information from sun's website but still don't
understand it
could anyone give me an example when NullPointerException been thrown?

Patricia Shanahan

2007-11-13, 10:14 pm

jack90830@gmail.com wrote:
> //template for checking combinations of card
>
> import javax.swing.*;
> public class template {
> public static void main(String[] args)
> {
> Card handCard[] = new Card[5];
> for(int i=0;i<5;i++)
> {
> handCard[i].num = 1 + (int)(Math.random( ) * 13);

....

new Card[5] is a reference to a newly created array of null references
to Card.

Each handCard element remains null until you do something such as:

handCard[i] = new Card();

Patricia
Knute Johnson

2007-11-13, 10:14 pm

jack90830@gmail.com wrote:
> //template for checking combinations of card
>
> import javax.swing.*;
> public class template {
> public static void main(String[] args)
> {
> Card handCard[] = new Card[5];
> for(int i=0;i<5;i++)
> {
> handCard[i].num = 1 + (int)(Math.random( ) * 13);
> handCard[i].suit = 1 + (int)(Math.random( ) * 4);
> System.out.println(handCard[i].num);
> System.out.println(handCard[i].suit);
> }
> System.exit(0);
> }
>
> public static class Card
> {
> Card(int n, int s)
> {
> n = 0;
> s = 0;
> }
> public int num = 0;
> public int suit =0;
> }
> }
> ==============================
>
> java.lang.NullPointerException
> at template.main(template.java:10)
> Exception in thread "main"
> ===============================
>
> -----------------------------------------------------------
> //from sun's website
> Thrown when an application attempts to use null in a case where an
> object is required. These include:
> Calling the instance method of a null object.
> Accessing or modifying the field of a null object.
> Taking the length of null as if it were an array.
> Accessing or modifying the slots of null as if it were an array.
> Throwing null as if it were a Throwable value.
> ----------------------------------------------------------------------
> i found those information from sun's website but still don't
> understand it
> could anyone give me an example when NullPointerException been thrown?
>


You have an array of Card but no Cards. Put in a line;

> for(int i=0;i<5;i++)
> {
> handCard[i].num = 1 + (int)(Math.random( ) * 13);
> handCard[i].suit = 1 + (int)(Math.random( ) * 4);


handCard[i] = new Card(
1 + (int)(Math.random() * 13),
1 + (int)(Math.random() * 4));

> System.out.println(handCard[i].num);
> System.out.println(handCard[i].suit);
> }


And look at the Random class, it has a bunch very handy methods for this.

--

Knute Johnson
email s/nospam/knute/
jack90830@gmail.com

2007-11-14, 4:31 am

thx guys


Roedy Green

2007-11-14, 4:31 am

On Wed, 14 Nov 2007 02:54:55 -0000, jack90830@gmail.com wrote, quoted
or indirectly quoted someone who said :

>could anyone give me an example when NullPointerException been thrown?


see
http://mindprod.com/jgloss/runerror...OINTEREXCEPTION
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Are Nybakk

2007-11-14, 4:31 am

jack90830@gmail.com wrote:
> //template for checking combinations of card
>
> import javax.swing.*;
> public class template {
> public static void main(String[] args)
> {
> Card handCard[] = new Card[5];
> for(int i=0;i<5;i++)
> {
> handCard[i].num = 1 + (int)(Math.random( ) * 13);
> handCard[i].suit = 1 + (int)(Math.random( ) * 4);
> System.out.println(handCard[i].num);
> System.out.println(handCard[i].suit);
> }
> System.exit(0);
> }
>
> public static class Card
> {
> Card(int n, int s)
> {
> n = 0;
> s = 0;
> }
> public int num = 0;
> public int suit =0;
> }
> }
> ==============================
>
> java.lang.NullPointerException
> at template.main(template.java:10)
> Exception in thread "main"
> ===============================
>
> -----------------------------------------------------------
> //from sun's website
> Thrown when an application attempts to use null in a case where an
> object is required. These include:
> Calling the instance method of a null object.
> Accessing or modifying the field of a null object.
> Taking the length of null as if it were an array.
> Accessing or modifying the slots of null as if it were an array.
> Throwing null as if it were a Throwable value.
> ----------------------------------------------------------------------
> i found those information from sun's website but still don't
> understand it
> could anyone give me an example when NullPointerException been thrown?
>


Must be the most common error there is. You have an object that didn't
initialize correctly. When you use that object later you get this exception.
Roedy Green

2007-11-16, 7:17 pm

On Wed, 14 Nov 2007 02:54:55 -0000, jack90830@gmail.com wrote, quoted
or indirectly quoted someone who said :

> {
> Card handCard[] = new Card[5];


see http://mindprod.com/jgloss/array.html#ALLOCATINGOBJECTS
for how to allocate and initialise an array of objects.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com