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

  • SEARCH
  • Home
  • 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 5964873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:30:42+00:00 2026-05-22T19:30:42+00:00

I’m having a problem reading the data from a file. When I try to

  • 0

I’m having a problem reading the data from a file.

When I try to calculate the GPA after inputting from the file I only get the name and the ID right. check the full code here

Name: Ali Ahmad
ID: 2009000

Exception in thread "main" java.lang.NullPointerException
    at IcsProject.display(IcsProject.java:136)
    at IcsProject.main(IcsProject.java:45)

`

I really need help with this. It took me hours to fix it but I could not.

public static void readFromFile() throws IOException
{
    System.out.println("Enter the file name ");
        String FileNameInput = keyboard.next();
        Scanner filein = new Scanner(new FileInputStream(FileNameInput));


        stuName = filein.nextLine();
        ID = filein.nextInt();

        filein.next();
        semNum = filein.nextInt();


    courseCode=new String[semNum][];
    creditHours=new int[semNum][];
    grade=new String[semNum][];
    semCode=new int[semNum];

    for(int i = 0; i < semNum; i++)
    {
            semCode[i] = filein.nextInt();

            filein.next();
            semCourses = filein.nextInt();

            for(int j = 0; j < semCourses; j++)
            {
              courseCode[i][j]=filein.next();
              creditHours[i][j]=filein.nextInt();
              grade[i][j]=filein.next();
            }
            }
    }

The input data is like this:

Ali Ahmad
2009000
Semesters 2

093
Courses 2

IAS100  2  A+
PE100   2  B

101
Courses 4

ICS103  3  A+
MATH101 4  B
PHYS101 4  C+
CHEM101 4  D+

This is the method calculating the GPA,etc it works fine with manual input.

public static void display(PrintWriter output)
{
 output.println("Name: "+stuName);
 output.println("ID: "+ID);
 output.println("");

 GPA=new double[semNum];
 for(int i=0;i<semNum;i++)
 {
 GPA[i]=0.0;
 double creditHoursSum=0.0;
 for(int j=0;j<courseCode[i].length;j++)
 {
  GPA[i]+=creditHours[i][j]*gradeValue(grade[i][j]);
  creditHoursSum+=creditHours[i][j];
 }
 GPA[i]=GPA[i]/creditHoursSum;
 }
 cumulativeGPA=0.0;
 for(int i=0;i<semNum;i++)
 cumulativeGPA+=GPA[i];
 cumulativeGPA=cumulativeGPA/semNum;
 for(int i=0 ; i < semNum ; i++)
 output.printf("GPA for semester %d  = %.2f\t%s\n",semCode[i],GPA[i],gpaLvl(GPA[i]));//Using `Printf` To limit the number of digits of the GPA. I learned the method from our Course Book.Took me a while to get it though.
 output.printf("\nCumulative GPA = %.2f\n",cumulativeGPA);
 output.println("");
}
  • 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-05-22T19:30:43+00:00Added an answer on May 22, 2026 at 7:30 pm

    You have to initialize your “sub”arrays correctly, i.e. inside your i-loop but right before the j-loop add the following lines:

    courseCode[i] = new String[semCourses];
    creditHours[i] = new int[semCourses];
    grade[i] = new String[semCourses];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.