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 8900501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:06:44+00:00 2026-06-15T01:06:44+00:00

Hi the following below works package com.ahmad.actionBar; import java.util.ArrayList; import android.app.Fragment; import android.os.Bundle; import

  • 0

Hi the following below works

package com.ahmad.actionBar;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragMent1 extends Fragment {

    private String arry[] = { "Tofeeq", "Ahmad", "Fragment", "Example",
            "Tofeeq", "Ahmad", "Fragment", "Example" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ListView listView = new ListView(getActivity());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1);

        for (String str : arry)
            array.add(str);
        listView.setAdapter(array);
        return listView;
    }
}

But when I’m trying to populate my array with string objetcs it doesn’t work

package com.ahmad.actionBar;

import java.util.ArrayList;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FragMent1 extends Fragment {

    private String bookNames[];

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ListView listView = new ListView(getActivity());
        ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1);

        bookNames= bookNames();//This doesn't work
        for (String str : bookNames)//This doesn't work
            array.add(str);
        listView.setAdapter(array);
        return listView;
    }

    private String[] bookNames(){
        SimpleBookManager testBook = new SimpleBookManager();
        for(int i=0; i < testBook.count(); i++){
        bookNames[i]= testBook.getBook(i).getTitle();
    }
        return bookNames;
    }
}

I get following errors NullpointeException??

11-14 23:36:54.531: E/AndroidRuntime(1949): FATAL EXCEPTION: main
11-14 23:36:54.531: E/AndroidRuntime(1949): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmad.actionBar/com.ahmad.actionBar.ActionBarMain}: java.lang.NullPointerException
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.os.Looper.loop(Looper.java:137)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at dalvik.system.NativeStart.main(Native Method)
11-14 23:36:54.531: E/AndroidRuntime(1949): Caused by: java.lang.NullPointerException
11-14 23:36:54.531: E/AndroidRuntime(1949):     at com.ahmad.actionBar.FragMent1.bookNames(FragMent1.java:39)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at com.ahmad.actionBar.FragMent1.onCreateView(FragMent1.java:29)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.BackStackRecord.run(BackStackRecord.java:635)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.Activity.performStart(Activity.java:5017)
11-14 23:36:54.531: E/AndroidRuntime(1949):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)

Here’s my SimpleBookManager class and I populate it manually so it shold’t be null.

package com.ahmad.actionBar;

import java.util.ArrayList;

public class SimpleBookManager implements BookManager {
    private ArrayList<Book> allBooks = new ArrayList<Book>();

    public ArrayList<Book> getAllBooks(){
    return  allBooks;
    }

    public int count(){
        return getAllBooks().size();
    }


    public Book getBook(int index){

        return allBooks.get(index);
    }
    public Book createBook(){
        Book book = new Book();
        allBooks.add(book);
        return book;
    }
    public void removeBook(Book book){
        allBooks.remove(book);
        //Remove instance of book
    }
    public void moveBook (int from, int to){
        Book book1 = allBooks.get(from);
        Book book2 = allBooks.get(to);
        allBooks.add(to, book1);
        allBooks.add(from, book2);

    }
    public int getMinPrice(){
    ArrayList<Integer> allPrices = getAllPrices();
    int smallestElem=allPrices.get(0);
    for(int i=0; i < allPrices.size(); i++){
         if (smallestElem > allPrices.get(i)){
             smallestElem = allPrices.get(i);
         }
      }
        return smallestElem;


    }
    public int getMaxPrice(){
        ArrayList<Integer> allPrices = getAllPrices();
        int biggestElem=allPrices.get(0);
        for(int i=0; i < allPrices.size(); i++){
             if (biggestElem < allPrices.get(i)){
                 biggestElem = allPrices.get(i);
             }
          }
            return biggestElem; 
    }
    public float getMeanPrice(){
        ArrayList<Integer> allPrices = getAllPrices();
        int total=0;
        for(int i=0; i < allPrices.size(); i++){
             total+=allPrices.get(i);
          }
            return total/allPrices.size();  

    }
    public int getTotalCost(){
        ArrayList<Integer> allPrices = getAllPrices();
        int total=0;
        for(int i=0; i < allPrices.size(); i++){
             total+=allPrices.get(i);
          }
            return total;
    }
    public void saveChanges(){
        //What to do here
    }
    private ArrayList<Integer> getAllPrices(){
        int totalElements = allBooks.size();
        ArrayList<Integer> allBookPrices = new ArrayList<Integer>();
        //loop through it
        for(int i=0; i < totalElements; i++){
          allBookPrices.add(allBooks.get(i).getPrice());
        }
        return allBookPrices;
    }

    public SimpleBookManager(){
        Book harryPotter1 = createBook();
        Book harryPotter2 = createBook();


        harryPotter1.setAuthor("JK Rowling");
        harryPotter1.setCourse("Harry Potter Kunskap");
        harryPotter1.setPrice(199);
        harryPotter1.setTitle("Harry Potter and the philosifer Stone");
        harryPotter1.setIsbn("9780590353403");

        harryPotter2.setAuthor("JK Rowling");
        harryPotter2.setCourse("Harry Potter Kunskap");
        harryPotter2.setPrice(299);
        harryPotter2.setTitle("Harry Potter and snake");
        harryPotter2.setIsbn("0439064872");
    }
}

Has probably something to do with List like this example here but I can’t figure out the exact syntax in my example so any help would be very much appriciated.

  • 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-15T01:06:45+00:00Added an answer on June 15, 2026 at 1:06 am

    In your code you do not initialize bookNames array

    private String[] bookNames(){
        SimpleBookManager testBook = new SimpleBookManager();
        bookNames = new String[testBook.count ()]; // this is what you should add
        for(int i=0; i < testBook.count(); i++){
            bookNames[i]= testBook.getBook(i).getTitle();
        }
        return bookNames;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code below which works, but I want to insert values
The following code works in all browser but IE8 and below. // Create Paragraph
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
I am confused how the following works in MySQL. In the queries below, the
I've the following code in my displayandmove.as file: package { import flash.display.MovieClip; public class
I wrote the basic code below and saved to a file called pdf.java. package
[Edit] I've summarized the answer to the following below, the error lies in the
I have the following code below however am confused as to why the div
I have the following code below for list into master page <div id=header> <ul>
Given the following code below, how can I have it modified such that 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.