AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[débutant]error NullPointerException

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Language Java (Français)
View previous topic :: View next topic  
Author Message
yoyo
Guest





PostPosted: Fri Apr 27, 2007 5:05 pm    Post subject: [débutant]error NullPointerException Reply with quote



Bonjour,

Sur ce news je vois que tout le monde est très a l'aise en java, donc je ne
sais pas si ma question a vraiment sa place.

Voilà je suis débutant en programmation et en java, je viens d'essayé de
programmer un petit programme, afin de m'entraîner en java.

Apparamment je m'embrouille un peu avec des différentes class, car j'ai une
error NullPointerException.

merci de votre aide




class Case {

char c;
}
class TableauCase{

Case [][]tabCase;

public TableauCase(){

for(int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase=new Case [i][j];
}
}
}

public void initialise(){

for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase[i][j].c='?';
}
}
}


public void affiche(){
Terminal.ecrireChar(tabCase[0][1].c);
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
Terminal.ecrireChar(tabCase[i][j].c);
}
}
}

}
Back to top
Dam
Guest





PostPosted: Fri Apr 27, 2007 5:05 pm    Post subject: Re: error NullPointerException Reply with quote



On 27 avr, 14:05, yoyo <y...@invalid.fr> wrote:
Quote:
Bonjour,

Sur ce news je vois que tout le monde est très a l'aise en java, donc je ne
sais pas si ma question a vraiment sa place.

Voilà je suis débutant en programmation et en java, je viens d'essayé de
programmer un petit programme, afin de m'entraîner en java.

Apparamment je m'embrouille un peu avec des différentes class, car j'ai une
error NullPointerException.

merci de votre aide

class Case {

char c;}

class TableauCase{

Case [][]tabCase;

public TableauCase(){

for(int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase=new Case [i][j];
}
}
}

public void initialise(){

for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase[i][j].c='?';
}
}
}

public void affiche(){
Terminal.ecrireChar(tabCase[0][1].c);
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
Terminal.ecrireChar(tabCase[i][j].c);
}
}
}

}

Un NullPointerException à quelle ligne???
Back to top
yoyo
Guest





PostPosted: Fri Apr 27, 2007 5:16 pm    Post subject: Re: error NullPointerException Reply with quote



Dam wrote:

Quote:
On 27 avr, 14:05, yoyo <y...@invalid.fr> wrote:
Bonjour,

Sur ce news je vois que tout le monde est très a l'aise en java, donc je
ne sais pas si ma question a vraiment sa place.

Voilà je suis débutant en programmation et en java, je viens d'essayé de
programmer un petit programme, afin de m'entraîner en java.

Apparamment je m'embrouille un peu avec des différentes class, car j'ai
une error NullPointerException.

merci de votre aide

class Case {

char c;}

class TableauCase{

Case [][]tabCase;

public TableauCase(){

for(int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase=new Case [i][j];
}
}
}

public void initialise(){

for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase[i][j].c='?';
}
}
}

public void affiche(){
Terminal.ecrireChar(tabCase[0][1].c);
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
Terminal.ecrireChar(tabCase[i][j].c);
}
}
}

}

Un NullPointerException à quelle ligne???


désolé, ligne 23 et 49 , dans ce cas je met le code complet.

================code =====================
class Case {

char c;
}
class TableauCase{

Case [][]tabCase;

public TableauCase(){

for(int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase=new Case [i][j];
}
}
}

public void initialise(){

//tabCase[0][1].c='?';
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase[i][j].c='?';
}
}
}


public void affiche(){
Terminal.ecrireChar(tabCase[0][1].c);
/*for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
Terminal.ecrireChar(tabCase[i][j].c);
}
}*/
}

}



class test{


public static void main(String [] args){
Case un= new Case();
TableauCase deux=new TableauCase();

deux.initialise();
deux.affiche();


}
}
Back to top
ricoh51
Guest





PostPosted: Fri Apr 27, 2007 6:05 pm    Post subject: Re: error NullPointerException Reply with quote

Quote:
Apparamment je m'embrouille un peu avec des différentes class, car j'ai
une error NullPointerException.


================code =====================
class Case {

char c;
}
class TableauCase{

Case [][]tabCase;

Salut

Case [][]tabCase=new Case[2][2];

en java, tu dois créer tes objets avant de pouvoir les utiliser, là tu
cherchais à utiliser un objet tableau non créé

bon courage

eric

--
http://ricoh51.free.fr/
Back to top
yoyo
Guest





PostPosted: Sat Apr 28, 2007 12:36 am    Post subject: Re: error NullPointerException Reply with quote

ricoh51 wrote:

Quote:
Apparamment je m'embrouille un peu avec des différentes class, car j'ai
une error NullPointerException.


================code =====================
class Case {

char c;
}
class TableauCase{

Case [][]tabCase;

Salut

Case [][]tabCase=new Case[2][2];

en java, tu dois créer tes objets avant de pouvoir les utiliser, là tu
cherchais à utiliser un objet tableau non créé

bon courage

eric



en effet, dans ma boucle j'avais fais une new Case[i][j] pour créer les
différents object du tableau mais j'ai pas créer le tableau, par contre
j'ai toujours la même erreur.
Back to top
yoyo
Guest





PostPosted: Sat Apr 28, 2007 3:00 am    Post subject: Re: error NullPointerException Reply with quote

ricoh51 wrote:

Quote:
Apparamment je m'embrouille un peu avec des différentes class, car j'ai
une error NullPointerException.


================code =====================
class Case {

char c;
}
class TableauCase{

Case [][]tabCase;

Salut

Case [][]tabCase=new Case[2][2];

en java, tu dois créer tes objets avant de pouvoir les utiliser, là tu
cherchais à utiliser un objet tableau non créé

bon courage

eric


il semblerais que l'erreur soit pas la meme

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at TableauCase.<init>(Case.java:15)
at test.main(Case.java:50)


On dirait qu'il essaye d'accéder une case de tableau qui n'existe pas!!!
Comprend pas pourquoi ?
>
Back to top
Maxime Daniel
Guest





PostPosted: Sat Apr 28, 2007 3:48 pm    Post subject: Re: error NullPointerException Reply with quote

On 28 avr, 00:00, yoyo <y...@invalid.fr> wrote:
Quote:
On dirait qu'il essaye d'accéder une case de tableau qui n'existe pas!!!
Exactement. La première en fait.
Comprend pas pourquoi ?
Moi non plus... Mais ce serait beaucoup plus facile avec le source. En

Java comme avec d'autres langages de programmation, la mise-au-point
d'un programme nécessite d'avoir le code. Et la discussion avec
d'autres programmeurs commence par là (un grand avantage des projets
'open source').
Cela mis à part, utilisez-vous un environnement de développement
intégré ? Il en existe de très performants qui sont gratuits (Eclipse,
NetBeans), et qui vous permettront de lancer votre programme pas-à-pas
pour examiner ce qui se passe.
Bon courage.
Back to top
ricoh51
Guest





PostPosted: Sat Apr 28, 2007 4:48 pm    Post subject: Re: error NullPointerException Reply with quote

Salut, je te mets une version qui tourne
2 remarques :
Quand tu écris
for(int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase=new Case [i][j];
}
tu crées 4 objets tabCase, et tu gardes la référence du dernier
seulement, il faut juste faire
Case tabCase[][]=new Case [2][2];
pour créer ton tableau

Et là encore tu utilisais des objets non créés, les Case... il faut les
créer avant de t'en servir

eric

--------------- Code qui fonctionne -----------------------
package test;

class Case {
char c;
}
class TableauCase{
Case tabCase[][]=new Case [2][2];

public TableauCase(){
}

public void initialise(){
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
tabCase[i][j]=new Case();
tabCase[i][j].c='?';
}
}
}

public void affiche(){
for (int i=0; i<2; i++){
for( int j=0; j<2; j++){
System.out.print(tabCase[i][j].c);
}
}
}

}
public class Main {

public Main() {
}

public static void main(String[] args) {
TableauCase deux=new TableauCase();

deux.initialise();
deux.affiche();
}
}

--
http://ricoh51.free.fr/
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Language Java (Français) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.