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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:34:37+00:00 2026-06-10T12:34:37+00:00

Main.java public class Main extends Activity implements onClickListener{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

  • 0

Main.java

public class Main extends Activity implements onClickListener{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    Toast.makeText(this ,testingShow(), Toast.LENGTH_SHORT).show(); //*1
    ...
    /* //*2
     *  MyProcessData hey = new MyProcessData();
     *  String yo = hey.sameTestingShow();
     *  Toast.makeText(this,yo, Toast.LENGTH_SHORT).show(); 
     */
}
public String testingShow(){
     StringBuilder appVariableDataToInitialize = new StringBuilder();
        BufferedReader reader=null;
        String line = "";
        try {
            reader = new BufferedReader(new FileReader(
                    new File(getFilesDir()+File.separator + DATA_FILE)));
            while ( (line = reader.readLine() )!= null ){
                appVariableDataToInitialize.append(line + "\n");
            }
        reader.close();
            
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "nofile" , Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            // 
            e.printStackTrace();
        }
    
    return appVariableDataToInitialize.toString();
 }

}

MyProcessData.java

public class MyProcessData extends MainActivity{

...

public String sameTestingShow(){
      StringBuilder appVariableDataToInitialize = new StringBuilder();
        BufferedReader reader=null;
        String line = "";
        try {
            reader = new BufferedReader(new FileReader(
                    new File(getFilesDir()+File.separator + DATA_FILE)));
            while ( (line = reader.readLine() )!= null ){
                appVariableDataToInitialize.append(line + "\n");
            }
        reader.close();
            
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "nofile" , Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            // 
            e.printStackTrace();
        }
    
    return appVariableDataToInitialize.toString();
 }

}

logcat

08-30 13:21:00.245: D/AndroidRuntime(2112): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-30 13:21:00.245: D/AndroidRuntime(2112): CheckJNI is ON
08-30 13:21:00.325: D/AndroidRuntime(2112): --- registering native functions ---
08-30 13:21:00.675: D/AndroidRuntime(2112): Shutting down VM
08-30 13:21:00.675: D/dalvikvm(2112): Debugger has detached; object registry had 1 entries
08-30 13:21:00.685: I/AndroidRuntime(2112): NOTE: attach of thread 'Binder Thread #3' failed
08-30 13:21:00.995: D/AndroidRuntime(2120): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-30 13:21:00.995: D/AndroidRuntime(2120): CheckJNI is ON
08-30 13:21:01.075: D/AndroidRuntime(2120): --- registering native functions ---
08-30 13:21:01.425: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.easylog/.MainActivity }
08-30 13:21:01.436: I/ActivityManager(58): Start proc com.easylog for activity com.easylog/.MainActivity: pid=2126 uid=10040 gids={}
08-30 13:21:01.465: D/AndroidRuntime(2120): Shutting down VM
08-30 13:21:01.475: D/dalvikvm(2120): Debugger has detached; object registry had 1 entries
08-30 13:21:01.955: I/global(2126): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
08-30 13:21:01.995: D/AndroidRuntime(2126): Shutting down VM
08-30 13:21:01.995: W/dalvikvm(2126): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-30 13:21:02.025: E/AndroidRuntime(2126): FATAL EXCEPTION: main
08-30 13:21:02.025: E/AndroidRuntime(2126): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.easylog/com.easylog.MainActivity}: java.lang.NullPointerException
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.os.Looper.loop(Looper.java:123)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invoke(Method.java:521)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at dalvik.system.NativeStart.main(Native Method)
08-30 13:21:02.025: E/AndroidRuntime(2126): Caused by: java.lang.NullPointerException
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.easylog.MyProcessData.sameTestingShow(MyProcessData.java:56)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.easylog.MainActivity.onCreate(MainActivity.java:54)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-30 13:21:02.025: E/AndroidRuntime(2126):     ... 11 more
08-30 13:21:02.025: W/ActivityManager(58):   Force finishing activity com.easylog/.MainActivity
08-30 13:21:02.535: W/ActivityManager(58): Activity pause timeout for HistoryRecord{450a1690 com.easylog/.MainActivity}
08-30 13:21:04.235: I/Process(2126): Sending signal. PID: 2126 SIG: 9
08-30 13:21:04.255: I/ActivityManager(58): Process com.easylog (pid 2126) has died.
08-30 13:21:04.265: W/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45058308
08-30 13:21:12.781: W/ActivityManager(58): Activity destroy timeout for HistoryRecord{450a1690 com.easylog/.MainActivity}
08-30 13:22:54.366: D/dalvikvm(171): GC_EXPLICIT freed 153 objects / 6128 bytes in 97ms

When i run this code, it perfectly worked.
But after i commented *1 in Main.java and uncommented *2 also in Main.java, i get an error java.lang.NullPointerException .
What does it means? it is the exactly the same function (same code),so i dont know where is the problem. i am beginner in programming,please be gentle.

EDIT => SOLVED

1.add constructor passing context to MyProcessData.java

private final Context myC;

public MyProcessData(Context c) {
    myContext = c;
}

2.edit MyProcessData => sameTestingShow()

reader = new BufferedReader(new FileReader(
                new File(myContext.getFilesDir()+File.separator + DATA_FILE)));
  • 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-10T12:34:38+00:00Added an answer on June 10, 2026 at 12:34 pm

    You cannot initialize MyProcessData with a new call. Any Activity should only be initialized by the framework so that context is proper.
    In your case you cannot do this

    MyProcessData hey = new MyProcessData();
    

    It is a bad idea to call a method of another Activity , Since the context will be not initialized.

    If you are very insistent.

    Then pass context like below

    MyProcessData hey = new MyProcessData(this); 
    

    and then use this context to get directory context.getFileDir()

    • 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 in Main.java : public class Main extends Activity implements
I have the following Java code: public class FirstActivity extends Activity implements OnClickListener {
package com.em.progressb; public class progressb extends Activity implements OnClickListener { ProgressDialog dialog; int increment;
in java we can do this: public class A{ public static void main(String...str){ B
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.lang.System; public class Splitter{ public static void main(String args[]){ String path_separator = Path
import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]

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.