Vì sao em làm đúng mà code không chạy?

package TEST6;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;

public class writeoject {

	public static void main(String[] args) throws IOException {
		File file = new File("nhanvien.txt");
		
		if(!file.exists())
		{
			file.createNewFile();
		}
		
		ArrayList<nhanvien> a = new ArrayList<nhanvien>();
		FileOutputStream fos = new FileOutputStream(file);
		ObjectOutputStream out = new ObjectOutputStream(fos);
		for(int i = 0;i<1;i++)
		{
			String manv;
			String ten;
			int tuoi;
			float luong;
			Scanner sc = new Scanner(System.in);
			System.out.println("Ma Nhan Vien : ");
			manv = sc.nextLine();
			System.out.println("Ten : ");
			ten = sc.nextLine();
			System.out.println("Tuoi : ");
			tuoi = sc.nextInt();
			System.out.println("luong : ");
			luong = sc.nextFloat();
			nhanvien nv = new nhanvien(manv, ten, tuoi, luong);
			a.add(nv);
		}
		out.writeObject(a);
		out.close();
		fos.close();        
	}
	
	


}

Sao dám khẳng định là đúng nhỉ, phải có chỗ sai sót thì mới không chạy theo ý định chứ.
Lớp nhanvien của bạn có implements Serializable chưa?

3 Likes

Vì em làm sai, hoặc sách dạy sai :))))

1 Like

“Vì sao em làm đúng mà em lại làm sai?”
IDE java tất nhiên sẽ có chỗ để em đặt breakpoint và chạy debug. Hãy tìm hiểu nó.

2 Likes

làm đúng mà nó ko chạy thì là tính năng chứ đâu phải bug :joy:

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?