program error when enter data in array and display null

前端 未结 2 1290
北海茫月
北海茫月 2021-01-29 16:52

i have done some correction by referring in here, but still haven quite satisfy of my program and it still not complete.
Problem 1 = my displayReg() comes out \"nullnullnull

相关标签:
2条回答
  • 2021-01-29 16:54
    int i = 0;
    Register[] a = new Register[i];   
    

    You are effectively attempting to create an array of 0 size. But when you try to access a[i] when i = 0, it attempts to access the element at 1'st position in the array (array have 0 as the first position).

    Also you have put it in a while loop which means you create a new array of 0 size on every loop cycle.

    try putting i = 10 (or whatever your program logic says) and put the array creation out of the loop

    0 讨论(0)
  • 2021-01-29 17:12

    Register[] a = new Register[i]; - creates an array with 0 items.

    a[i].Reg(); - access to the position 0 of an array that got no items at all.

    Here is a tutorial

    0 讨论(0)
提交回复
热议问题