Aller au contenu
View in the app

A better way to browse. Learn more.

Next

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Java] java.lang.NullPointerException

Featured Replies

Posté(e)

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);
}
}

Modifié par Premium

Archivé

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.