Aller au contenu

[Java] java.lang.NullPointerException


Premium

Messages recommandés

Bonjour,

A l'execution de mon programme, j'obtiens :

java.lang.NullPointerException
	at Test.<init>(Test.java:213)
	at Main.main(Main.java:7)

La ligne 213 est la suivante :

table.setModel(model);

Pourquoi est-ce que ça indique ce message alors que model a bien été initialisé ?

Le classe entière :

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.JScrollPane;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.print.PrinterException;
import java.io.Serializable;
import java.io.File;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;

public class Test extends JPanel implements Serializable{
Object [][] donnees;
String [] titreColonnes;
JTable table;
DefaultTableModel model;

public Test(){
try{
	File file = new File("sauve.tmp");
	//deserialisation
	if(file.exists())
	{
		try {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("sauve.tmp"));
		try {

			Vector data = (Vector) ois.readObject();
			Vector columnIdentifiers = new Vector();
			for(int i=1; i<=52; i++)
			columnIdentifiers.add("semaine "+i);
			((DefaultTableModel)table.getModel()).setDataVector(data, columnIdentifiers);
		} finally {
			ois.close();
		}
		} catch(IOException ioe) {
		ioe.printStackTrace();
		}
	}

	else
	{
		donnees = new Object [][]{
		{"lundi", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
		{"8:00", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""},
		//...
		};

		titreColonnes = new String[] {"",
					  "semaine 1",
					  "semaine 2",
					  //...
		};

		model = new DefaultTableModel(donnees, titreColonnes);
		table.setModel(model);


		for(int nbcol =1; nbcol<=52; nbcol++){
		TableColumn cC = table.getColumnModel().getColumn(nbcol);
		cC.setCellEditor(new Editeur());
		}

		JScrollPane ascenseurs = new JScrollPane(table);

		table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
		table.setRowHeight(40);
		ascenseurs.setPreferredSize(new Dimension(500,500));
		setLayout(new BorderLayout());
		add(ascenseurs, BorderLayout.CENTER);
	}
}catch(Exception e){
	e.printStackTrace();
}

/*JScrollPane ascenseurs = new JScrollPane(table);

table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
table.setRowHeight(40);
ascenseurs.setPreferredSize(new Dimension(500,500));
setLayout(new BorderLayout());
add(ascenseurs, BorderLayout.CENTER);*/
}

public void imprime(){
try {
	table.print(JTable.PrintMode.NORMAL);
}catch (PrinterException pe) {
	System.err.println("Error printing: " + pe.getMessage());
}
}

public void sauvegarder(){
if(table.isEditing()){
	if (table.getCellEditor() != null) {
	table.getCellEditor().stopCellEditing();
	}
}
//Serialisation
Vector data = ((DefaultTableModel)table.getModel()).getDataVector();
try {
	ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("sauve.tmp")); 
	try{
	oos.writeObject(data);
	}finally{
	oos.close();
	}
} catch (IOException ioe) { 
	ioe.printStackTrace();
}
}
}

La classe Main :

import javax.swing.JFrame;

public class Main{
public static void main(String [] args){
JFrame monCadre = new JFrame();
Menu menu = new Menu();
Test myTable = new Test();
monCadre.setContentPane(myTable);
monCadre.setJMenuBar(menu.getMenu(myTable));
//monCadre.setContentPane(myTable); 
monCadre.pack(); 
monCadre.setVisible(true);
monCadre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...