1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: java NullPointerException when running the program

Discussion in 'Software Development' started by sillyboy, Apr 24, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. sillyboy

    sillyboy Thread Starter

    Joined:
    May 30, 2007
    Messages:
    27
    It is a Java program I wrote, with so far no compile error. But there is the NullPointerException message when running the program. How to solve the problem?

    Exception in thread "main" java.lang.NullPointerException
    at TaskSet$Task.access$000(TaskSet.java:75)
    at TaskSet.getStartTime(TaskSet.java:72)
    at TaskSet.minCompletionTime(TaskSet.java:47)
    at TaskSetTest.main(TaskSetTest.java:11)
    Press any key to continue . . .

    Here my program

    import java.lang.Math;

    public class TaskSet
    {
    private final int MAX = 20;
    private Task tasks[];
    private int matrix[][];
    private int num;

    public TaskSet()
    {
    tasks = new Task[MAX];
    matrix = new int[MAX][MAX];
    num = 0;
    for (int i = 0; i < MAX; i++)
    {
    for (int j = 0; j < MAX; j++)
    {
    matrix[j] = 0;
    }
    }
    }

    public void addTask(int x)
    {
    tasks[num++] = new Task(x);
    }

    public void addTaskConstraint(int i, int j)
    {
    if (matrix[j] == 1)
    {
    tasks.startTime = -1;
    tasks[j].startTime = -1;
    return;
    }
    matrix[j] = 1;
    getStartTime(i);
    minCompletionTime();
    }

    public int minCompletionTime()
    {
    int time = 0;
    for (int i = 0; i < MAX; i++)
    {
    if (getStartTime(i) == -1)
    {
    return -1;
    }
    time = Math.max(time, getStartTime(i) + tasks.time);
    }
    return time;
    }

    public int getStartTime(int i)
    {
    for (int j = 0; j < MAX; j++)
    {
    if (matrix[j] == 1 && tasks[j].startTime != -1)
    {
    for (int k = 0; k < MAX; k++)
    {
    if (matrix[j][k] == 1 && tasks[k].startTime != -1)
    {
    tasks[j].startTime = Math.max(tasks[j].startTime, tasks[k].startTime + tasks[k].time);
    }
    }
    tasks.startTime = Math.max(tasks.startTime, tasks[j].startTime + tasks[j].time);
    }
    }
    return tasks.startTime;
    }

    private static class Task
    {
    private int time;
    private int startTime;

    public Task(int x)
    {
    time = x;
    startTime = 0;
    }
    }
    }

    public class TaskSetTest
    {
    public static void main(String[] args)
    {
    TaskSet tasks = new TaskSet();

    tasks.addTask(8);
    tasks.addTask(2);
    tasks.addTask(5);

    System.out.println(tasks.minCompletionTime());
    tasks.addTaskConstraint(0, 2);
    System.out.println(tasks.minCompletionTime());
    tasks.addTaskConstraint(0, 1);
    System.out.println(tasks.minCompletionTime());

    System.out.println(tasks.getStartTime(0));
    System.out.println(tasks.getStartTime(1));
    System.out.println(tasks.getStartTime(2));

    tasks.addTaskConstraint(1, 2);
    System.out.println(tasks.minCompletionTime());
    System.out.println(tasks.getStartTime(0));
    System.out.println(tasks.getStartTime(1));
    System.out.println(tasks.getStartTime(2));

    tasks.addTaskConstraint(1, 0);
    System.out.println(tasks.minCompletionTime());
    System.out.println(tasks.getStartTime(0));
    System.out.println(tasks.getStartTime(1));
    System.out.println(tasks.getStartTime(2));
    }
    }
     
  2. e2e8

    e2e8

    Joined:
    Dec 22, 2009
    Messages:
    81
    I was going to debug this for you but then I grew bored with that.
    Just follow the standard procedure for figuring out what is wrong.
    Add print statements, breakpoints etc.
    Debugging is a valuable skill to know
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/919009