java-NullPointerException

问题描述

NullPointerException

public class RandomArray {
private int arraySize;
private int sum;
private double average;
private int[] array; // instance variable
/**
* Constructor
* @param size The size of the array.
*/
public RandomArray(int size) {
// write your code here
arraySize = size;
int[] array = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = (int) Math.random()*10;
}
}

/**
 *  A method to print the array elements.
 */
public void printArray() {
    // write your code here
    for(int i = 0; i < arraySize; i++)
        System.out.print(array[i]);
}

/**
 *  A method to calculate the sum of all elements.
 *  @return  The sum.
 */
public int calcSum() {
    // write your code here
    sum = 0;
    for(int i = 0; i < arraySize; i++)
        sum += array[i];
    return sum;
}

/**
 *  A method to calculate the mean (or average) of all elements.
 *  @return  The mean.
 */
public double calcMean() {
    // write your code here
    average = this.calcSum() / arraySize;
    return average;
}

/**
 *  A main method to test.
 */
public static void main(String[] args) {
    // Check to see if the user has actually sent a parameter to the method.
    if (args.length != 1) {
        System.out.println("Usage: java RandomArray <NUM>. Example: java RandomArray 5");
    System.exit(-1);
}

// Create an instance of the class.
RandomArray test = new RandomArray(Integer.parseInt(args[0]));

// Print the array.
test.printArray();

// Calculate the sum of all the values in the array and print it.
System.out.println("Sum: " + test.calcSum());

// Calculate the mean of all the values in the array and print it.
System.out.println("Mean: " + test.calcMean());
}

}

错误报告
Exception in thread "main" java.lang.NullPointerException
at RandomArray.printArray(RandomArray.java:35)
at RandomArray.main(RandomArray.java:74)

解决方案

sum += array[i];
这里array在哪里定义的?哪里初始化的?

解决方案二:

你的程序args[0]是什么,你有传命令行参数给程序么?

参照 http://blog.csdn.net/randomnet/article/details/7416456
这里的做法,给一个参数

解决方案三:

onStartCommand NullPointerException

时间: 2024-09-15 23:46:22

java-NullPointerException的相关文章

JsonMappingException: (was java.lang.NullPointerException)

使用jackson 序列化Java对象的时候报异常: Java代码   com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.chanjet.gov.Student["age"])       at com.fasterxml.jackson.databind.JsonMappingException.

java.lang.NullPointerException: Expected timestamp

java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null java.lang.NullPointerException: Expected timestamp in the Flume event headers, but it was null 使用flume的时候出现的问题. sink是hdfs,然后使用目录自动生成功能.出现如题的错误,看官网文档说的是需要在每

JSP页面中出现的一个异常:org.apache.jasper.JasperException: java.lang.NullPointerException

先把出现的异常情况贴出来吧: type Exception report messagejava.lang.NullPointerException descriptionThe server encountered an internal error that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: java.lang.NullPointerException

图片-svn更新后,jsp页面报java.lang.NullPointerException

问题描述 svn更新后,jsp页面报java.lang.NullPointerException 请大神指点 谢谢 解决方案 svn一般要先更新再提交,有报错的话就先修改错误 解决方案二: 空指针异常,看看你同组的人有没有修改过东西,没有全部上传

java web java.lang.NullPointerException

问题描述 java web java.lang.NullPointerException 严重: Servlet.service() for servlet [selectServlet] in context with path [/Exercise] threw exceptionjava.lang.NullPointerException at DB.DataBaseConnection.selectOperation1(DataBaseConnection.java:45) at Ser

异常-java.lang.NullPointerException

问题描述 java.lang.NullPointerException 请问下这种异常怎么解决呢 解决方案 空指针错误.这种错误一般是由于: 1.使用的类对象为null 2.指针越界 3.配置文件配置路径有错,无法找到路径 解决方案二: SDK和ADT为22.6.2版本 工程为4.4.2 新建了一个工程 ,代码编写好后并没有报错,在安装支持后报出如下错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com

避免Java应用中NullPointerException的技巧和最佳实践

Java应用中抛出的空指针异常是解决空指针的最好方式,也是写出能顺利工作的健壮程序的关键.俗话说"预防胜于治疗",对于这么令人讨厌的空指针异常,这句话也是成立的.值得庆幸的是运用一些防御性的编码技巧,跟踪应用中多个部分之间的联系,你可以将Java中的空指针异常控制在一个很好的水平上.顺便说一句,这是Javarevisited上的第二个空指针异常的帖子.在上个帖子中我们讨论了Java中导致空指针异常的常见原因,而在本教程中我们将会学习一些Java的编程技巧和最佳实践.这些技巧可以帮助你避

android开发-java.lang.NullPointerException 在学习armenu时候遇到这问题的

问题描述 java.lang.NullPointerException 在学习armenu时候遇到这问题的 java.lang.NullPointerException at com.administrator_example.icm_arcmenu.view.ArcMenu.layoutButton(ArcMenu.java:137) 这句:mCButton.setOnClickListener(this); at com.administrator_example.icm_arcmenu.v

ndroid-新人求助 java.lang.NullPointerException

问题描述 新人求助 java.lang.NullPointerException 04-26 21:42:40.039: E/AndroidRuntime(1125): FATAL EXCEPTION: main 04-26 21:42:40.039: E/AndroidRuntime(1125): Process: com.example.remotecontrol, PID: 1125 04-26 21:42:40.039: E/AndroidRuntime(1125): java.lang

sharepreferences-安卓:java.lang.NullPointerException:

问题描述 安卓:java.lang.NullPointerException: logcat显示错误: FATAL EXCEPTION: main Process: org.droidplanner, PID: 21924 java.lang.RuntimeException: Unable to resume activity {org.droidplanner/org.droidplanner.android.activities.FlightActivity}: java.lang.Nul