baidu_31670843 2016-04-08 01:59 采纳率: 0%
浏览 1906

Java编写的售票系统问题求教

编程初学者,我用Java编写了一个简单的售票系统,但是运行时候报错,希望大牛能帮我看一下!
这是一个User类:

package subwayTicketSellingSystem;
import java.util.Scanner;

public class User
{
private String ticketType;
private Ticket tic;
private TicketMachine machine;
public double money;
public Ticket buyTicket;
public static void main(String[] args)
{
User user1 = new User();
System.out.println("Please choose your ticket type: ");
System.out.println("1: normal ticket");
System.out.println("2: day ticket");
Scanner ticketType = new Scanner(System.in);
String ticType = ticketType.nextLine();
switch(ticType)
{
case "1":
user1.tic = new norTicket();
break;
case "2":
user1.tic = new dayTicket();
}
System.out.println("Your ticket price is: " + user1.tic.calculatePrice());
System.out.println("Please pay for your tickets: ");
Scanner money = new Scanner(System.in);
String userMoney = money.nextLine();
if (user1.machine.collectMoney())
{
System.out.println("Your charge is: " + user1.machine.giveCharge());
user1.machine.printTicket();
}
else System.out.println("Mnoey is not enough, invalid operation.");
}
}
这是一个TicketMachine类:
package subwayTicketSellingSystem;

public class TicketMachine
{
User u1 = new User();
public Ticket Tickets[];
public double getTicketPrice()
{
return u1.buyTicket.calculatePrice();
}
public boolean collectMoney()
{
if ( u1.money >= u1.buyTicket.calculatePrice())
return true;
else return false;
}
public double giveCharge()
{
return u1.money - u1.buyTicket.calculatePrice();
}
public Ticket printTicket()
{
System.out.println("Your original station: ");
return null;
}
}
报错信息:
Exception in thread "main" java.lang.NullPointerException
at subwayTicketSellingSystem.User.main(User.java:31)
十分感谢!

  • 写回答

5条回答

  • 丵鹰 2016-04-08 02:10
    关注

    是的user1对象中的参数为null,你debug下,那个值为null

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料