1 Replies - 909 Views - Last Post: 26 September 2013 - 01:13 PM Rate Topic: -----

#1 daonlyillwiz   User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

java.io.Console output difficulties

Posted 26 September 2013 - 01:08 PM

Hello all, im trying to use console output as per my assignment but am having an error, here's the problem:
console=null
Exception in thread "main" java.lang.NullPointerException
at cs.CreditCard3.printCard(CreditCard3.java:58)
at cs.Test3.main(Test3.java:18)



package cs;

import java.io.Console;

public class CreditCard3 {
	// Instance variables:
	private String number;
	private String name;
	private String bank;
	private double balance;
	private int limit;

	// Constructor:
	CreditCard3(String no, String nm, String bk, double bal, int lim) {
		number = no;
		name = nm;
		bank = bk;
		balance = bal;
		limit = lim;
	}

	// Accessor methods:
	public String getNumber() {
		return number;
	}

	public String getName() {
		return name;
	}

	public String getBank() {
		return bank;
	}

	public double getBalance() {
		return balance;
	}

	public int getLimit() {
		return limit;
	}

	// Action methods:
	public boolean chargeIt(double price) { // Make a charge
		if (price + balance > (double) limit)
			return false; // There is not enough money left to charge it
		balance += price;
		return true; // The charge goes through in this case
	}

	public void makePayment(double payment) { // Make a payment
		balance -= payment;
	}

	public static void printCard(CreditCard3 c) { // Print a card's information
		Console console = System.console();
		System.out.println("console=" + console);
		console.writer().println("Number = " + c.getNumber());
		console.writer().println("Name = " + c.getName());
		console.writer().println("Bank = " + c.getBank());
		console.writer().println("Balance = " + c.getBalance()); // Implicit
																	// cast
		console.writer().println("Limit = " + c.getLimit()); // Implicit cast
	}
}



package cs313;

public class Test3 {
	public static void main(String[] args) {
		CreditCard3 wallet[] = new CreditCard3[10];
		wallet[0] = new CreditCard3("5391 0375 9387 5309", "John Bowman",
				"California Savings", 0.0, 2500);
		wallet[1] = new CreditCard3("3485 0399 3395 1954", "John Bowman",
				"California Federal", 0.0, 3500);
		wallet[2] = new CreditCard3("6011 4902 3294 2994", "John Bowman",
				"California Finance", 0.0, 5000);
		for (int i = 1; i <= 16; i++) {
			wallet[0].chargeIt((double) i);
			wallet[1].chargeIt(2.0 * i); // implicit cast
			wallet[2].chargeIt((double) 3 * i); // explicit cast
		}
		for (int i = 0; i < 3; i++) {
			CreditCard3.printCard(wallet[i]);
			while (wallet[i].getBalance() > 100.0) {
				wallet[i].makePayment(100.0);
				System.out.println("New balance = " + wallet[i].getBalance());

			}
		}
	}
}



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: java.io.Console output difficulties

#2 daonlyillwiz   User is offline

  • D.I.C Head

Reputation: -10
  • View blog
  • Posts: 150
  • Joined: 14-March 11

Re: java.io.Console output difficulties

Posted 26 September 2013 - 01:13 PM

Is this perhaps because System.console() returns null if your application is not run in a terminal?

Im compiling in Eclipse by the way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1