Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8604113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:29:23+00:00 2026-06-12T02:29:23+00:00

I use two class but i run: C Nome: Regis Idade: 25 Appear Exception

  • 0

I use two class but i run:

C

Nome: Regis

Idade: 25

Appear

Exception in thread “main” java.lang.NullPointerException
at registrosarraylist03xmlteste.RegistrosArrayList03_xmlteste.cadastrar(RegistrosArrayList03_xmlteste.java:72)
at registrosarraylist03xmlteste.RegistrosArrayList03_xmlteste.main(RegistrosArrayList03_xmlteste.java:23)
Java Result: 1

And lista.remove(p); not work

/*
 * ArrayList
 */
package registrosarraylist03xmlteste;

import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class RegistrosArrayList03_xmlteste {

    public static void main(String[] args) {
        ArrayList listaPessoas = new ArrayList();

        // Menu
        char op;
        do {
            op = menu();
            switch (op) {
                case 'C':
                case 'c': // cadastrar
                    cadastrar(listaPessoas);
                    gravarXML(listaPessoas, "teste.xml");
                    break;
                case 'L':
                case 'l': // ler
                    lerXML("teste.xml");
                    break;
                case 'E':
                case 'e': // editar
                    Scanner in = new Scanner(System.in);
                    MyPessoas p1 = new MyPessoas();
                    System.out.print("Digite o ID: ");
                    p1.ID = in.nextInt();
                    System.out.print("Novo nome: ");
                    p1.nome = in.next();
                    System.out.print("Nova idade: ");
                    p1.idade = in.nextInt();
                    editar(listaPessoas, p1);
                    gravarXML(listaPessoas, "teste.xml");
                    listar(listaPessoas);
                    break;
                case 'R':
                case 'r': // remover
                    System.out.println("O comando Remover ainda nao esta pronto.");
                    break;
                default:
                    System.out.println("Opcao invalida.");
                    break;
            }
        } while (op != 'S' && op != 's');
    }

    static char menu() {
        System.out.println("\n ### MENU ### Selecione uma opcao, digitando a primeira letra.");
        System.out.println("Cadastrar  Listar  Editar  Remover  Sair");
        System.out.print("Opcao: ");
        Scanner in = new Scanner(System.in);
        return in.next().charAt(0);
    }

    static void cadastrar(ArrayList lista) {
        Scanner in = new Scanner(System.in);
        MyPessoas p = new MyPessoas();
        // ID gerado automaticamente.
        System.out.print("Nome: ");
        p.nome = in.nextLine();
        System.out.print("Idade: ");
        p.idade = in.nextInt();
        System.out.print("Fone: ");
        p.fone.fone=in.next();
        lista.add(p);
    }

    static void listar(ArrayList lista) {
        System.out.println("ID - Nome - Idade");
        for (int i = 0; i < lista.size(); i++) {
            // Obtem os dados da lista MyPessoas
            MyPessoas p = (MyPessoas) lista.get(i);
            System.out.println(p.ID + "  - " + p.nome + " - " + p.idade);
        }
    }

    static ArrayList lerXML(String filename) {
        XStream xstream = new XStream();
        File arquivo = new File(filename);
        ArrayList listaPessoas = (ArrayList) xstream.fromXML(arquivo);
        listar(listaPessoas);
        return listaPessoas;
    }

    static void gravarXML(ArrayList lista, String filename) {
        XStream xstream = new XStream();
        String xml = xstream.toXML(lista);
        GravarLer Gravar = new GravarLer();
        GravarLer.gravar(xml, filename);
    }

    static void editar(ArrayList lista, MyPessoas p1) {
        System.out.print("Digite o nome: ");
        for (int i = 0; i < lista.size(); i++) {
            // Obtem os dados da lista MyPessoas
            MyPessoas p = (MyPessoas) lista.get(i);
            if (p.ID == p1.ID) {
                p.nome = p1.nome;
                p.idade = p1.idade;
                break;
            }
        }
    }
//    static void remover(ArrayList lista, MyPessoas p1) {
//        // Obtem os dados da lista MyPessoas
//        MyPessoas p = new MyPessoas();
//        System.out.print("Digite o ID: ");
//        rmvID = in.nextInt();
//        lista.remove(p);
//    }
}



/*
 * Classe de MyPessoas
 */
package registrosarraylist03xmlteste;

public class MyPessoas {

    public static int lastID = 1;
    public int ID;
    public String nome;
    public int idade;
    public Fone fone;

    // Adiciona numeracao sequencial ao ID.
    public MyPessoas() {
        ID = MyPessoas.lastID++;
    }
}



package registrosarraylist03xmlteste;

public class Fone {

    public String fone;
}



/*
 * Grava e Le registros em arquivos XML.
 */
package registrosarraylist03xmlteste;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

public class GravarLer {

    static void gravar(String xml, String filename) {
        try {
            FileWriter w = new FileWriter(filename);
            w.write(xml);
            w.flush();
            w.close();
        } catch (Exception e) {
            System.out.println("Erro ao gravar XML: " + e);
        }
    }

    static String ler(String filename) {
        try {
            Scanner in = new Scanner(new File(filename));
            StringBuilder sb = new StringBuilder();
            while (in.hasNext()) {
                sb.append(in.next());
            }
            in.close();
            return sb.toString();
        } catch (Exception e) {
            System.out.println("Erro ao ler XML: " + e);
        }
        return "";
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:29:25+00:00Added an answer on June 12, 2026 at 2:29 am

    You must initialize the Fone attribute in your MyPessoas class.
    In the constructor add:

    fone = new Fone();
    

    At a first glance this should solve your error but you may need to make other corrections.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run two Servlet-class in a single web.xml but its not
I use these two files here and here . I created a class in
We are two students who want to use one-class svm for dectection of summary
I'm trying to use the DSACryptoServiceProvider class with C# to create two DLLs: one
I have two questions - I'm a beginner in Java but have a huge
This could probably be generalized to any templated class, but I've run into this
I am trying to run a RPGLE program which use java methods. I am
I try to use jar command to pack a bunch of java class files
I have two threads running from a controller class. The first thread receives SMS
Is it possible to use an autogenerated class in the same run as it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.