GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Nov-2008, 13:30
MandySI MandySI is offline
New Member
 
Join Date: Nov 2008
Posts: 1
MandySI is on a distinguished road

Exception in thread "main" java.lang.NullPointerException error


I get an exception in thread "main" java.lang.NullPointerException error
with this program:

JAVA Code:
public class Tabla
{
	public char [][] tab;
	public char jucator; //indica A sau N, in functie de a cui jucator e tabla
	
	public Tabla (){}
	
	public Tabla (char juc) //constructor pentru tabla cu piesele in pozitiile initiale
	{
		int i, j, k;
		char [][]tab = new char[8][8];
		for ( i=0; i<6; i++)
			for ( j=0; j < 8; j++)
				tab[i][j]='O';
		for (k=0; k < 8; k++)
			tab[6][k]='P';
		tab[7][0]='T';
		tab[7][7]='T';
		tab[7][1]='C';
		tab[7][6]='C';
		tab[7][2]='N';
		tab[7][5]='N';
		tab[7][3]='D';
		tab[7][4]='R';
		jucator=juc;
	}
	
	public void showTabla()
	{
		for (int i=0; i<8; i++)
		{
		for (int j=0; j<8; j++)
			System.out.print(this.tab[i][j]+" ");
		System.out.println();	
		}
	}
}

JAVA Code:
import java.io.*;
import java.util.*;
import java.lang.*;

public class Test
{
	public static void main (String [] args)
	{
		Tabla tabla = new Tabla('A');
        tabla.showTabla();
		}
}
Last edited by LuciWiz : 19-Nov-2008 at 14:08. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 19-Nov-2008, 19:38
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,230
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: exception in thread "main" java.lang.NullPointerException error - please help


Problem is in the constructor...

You have created two tab arrays. Even though the tab array inside the constructor is initialized and set, the class-level tab array is NOT, and so it IS null.

This line in the constructor:
JAVA Code:
	char [][]tab = new char[8][8];
just simply needs to be:
JAVA Code:
	tab = new char[8][8];
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
Last edited by TurboPT : 19-Nov-2008 at 20:43.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NullPointerException valley1girl Java Forum 12 18-Jun-2009 13:32
Exception in thread "main" java.lang.NullPointerException gedcraw555 Java Forum 3 25-Feb-2008 09:22
Exception in thread "main" java.lang.NullPointerException Magelloo Java Forum 3 06-May-2007 00:01
Thread Stopping Exception Gix .NET Forum 12 28-Nov-2006 05:30
please help in solving: Exception in thread "main" java.lang.nullpointerexception vkiran_v Java Forum 8 09-May-2006 08:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.