简体   繁体   English

当我尝试从对象数组调用方法时,我的 Java 代码崩溃

[英]My Java code crashes when I try to call a method from an array of objects

I'm just learning Java and trying to have an array of a class.我只是在学习 Java 并试图拥有一个 class 的数组。 When a call a methed from the array it crashes.当从数组中调用一个方法时,它会崩溃。 Works fine if it is not an array如果它不是数组,则可以正常工作

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  cDate test=new cDate();
  test.setDay(0);
  mAppoitments = new cDate[24];
  // crashes why?????
  mAppoitments[0].setDay(0); 

You haven't filled your array with objects.你还没有用对象填充你的数组。 You have to:你必须:

  cDate[0] = test;

Otherwise you have null at index 0, and you cannot invoke anything on null .否则,您在索引 0 处有null ,并且您不能在null上调用任何内容。

And next time you ask a question, give all needed details:下次您提出问题时,请提供所有需要的详细信息:

  • what is the exception message and stacktrace.什么是异常消息和堆栈跟踪。 "crashes" means almost nothing “崩溃”几乎没有任何意义
  • tell us what are your variables that are not initialized in the code snippet.告诉我们您的哪些变量未在代码段中初始化。 You can see one answer that is telling you to fix a local declaration, which is probably an instance variable.您可以看到一个答案告诉您修复本地声明,这可能是一个实例变量。

You have an array of 24 objects, each of which is set to null .您有一个包含 24 个对象的数组,每个对象都设置为null You need to initialize each one before you can call methods on it.您需要先初始化每一个,然后才能对其调用方法。

You have initialized the array but not the objects in the array.您已初始化数组,但未初始化数组中的对象。 Try initializing these elements before using them.在使用它们之前尝试初始化这些元素。

mAppoitments = new cDate[24];
for (int i = 0; i < mAppoitments.length; i++)
    mAppoitments[i] = new cDate();
mAppoitments[0].setDay(0);
final int COUNT= 24;

mAppoitments = new cDate[COUNT];
for(int i = 0 ; i < COUNT ; ++i) {
   mAppoitments[i] = new cDate();
   mAppoitments[i].setDay(0); 
}
cDate myAppointments = new cDate[24];

try declaring the variable type尝试声明变量类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从活动类调用函数时崩溃 - crashes when I try to call function from activity class 当该方法单独运行时,我的代码会尝试工作,但是当我在测试程序时调用该方法时…出现捕获错误 - try of my code works when the method is run on its own, but when I call the method when testing my program… catch errors appears 当我尝试在主方法之外调用方法时发生NullPointerException - NullPointerException when I try to call a method outside my main method 当我从我的Javascript代码调用Java函数时,WebView崩溃了。 为什么? - When I invoke a Java function from my Javascript code, WebView crashes. Why? 尝试在 VS Code 中构建 Java 项目时出错 - Errors when I try to build my Java project in VS Code Java对象数组崩溃 - Java Array of Objects crashes 当我尝试从Firestore提取数据并显示在listview片段中时,我的应用程序崩溃 - My app crashes when i try to fetch data from Firestore and display in the listview fragment 当我尝试启动更改对象可见性的方法时,应用程序崩溃 - Application crashes when i try to start a method to change the visibility of an object 当我尝试解析字符串时,我的应用程序崩溃了 - My application crashes when I try to parse a string 当我尝试使用intent传递对象时,它会崩溃我的应用程序 - When I try to pass object with intent it crashes my application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM