在Android Studio中访问.xls文件时出错

时间:2018-10-27 16:08:55

标签: javascript android android-studio

我有一个名为“ question.xls”的文件。我正在尝试使用该sheet生成二维字符串数组。我已经将jxl.jar添加为资源库。这是我的代码和问题。这是MainActivity.java。我刚刚尝试访问然后将每个单元分配给二维数组

10-27 15:29:50.631 2719-2719/? D/Result: table details: 970  7
10-27 15:29:50.632 2719-2719/? D/AndroidRuntime: Shutting down VM
10-27 15:29:50.633 2719-2719/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bhagyo.excell, PID: 2719
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhagyo.excell/com.example.bhagyo.excell.MainActivity}: java.lang.NullPointerException: Attempt to write to null array
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.NullPointerException: Attempt to write to null array
        at com.example.bhagyo.excell.MainActivity.readQuestion(MainActivity.java:55)
        at com.example.bhagyo.excell.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:6975)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

我的详细日志已满,该错误主要是由于null数组引起的,但它在哪里

from django.shortcuts import render, HttpResponse, HttpResponseRedirect
from .models import Treasure, TamaniosCantidades
from .forms import TreasureForm, TamaniosCantidadesForm, LoginForm
from django.contrib.auth import authenticate, login, logout


# Create your views here.

def index(request):
    treasures = Treasure.objects.all()
    form = TreasureForm()
    tamanioscantidades_form = TamaniosCantidadesForm()
    return render(request, 'main_app/index.html', {'treasures': treasures,
                                                   'form': form,
                                                   'tamanioscantidades_form': tamanioscantidades_form})


def productos(request):
    treasures = Treasure.objects.all()
    form = TreasureForm()
    return render(request, 'main_app/productos.html', {'treasures': treasures,
                                                       'form': form})


def die_cut(request):
    tamanioscantidades_form = TamaniosCantidadesForm()
    return render(request, 'main_app/die-cut-stickers.html', {'tamanioscantidades_form': tamanioscantidades_form})


def post_tamanioscantidades(request):
    form = TamaniosCantidadesForm(request.POST)
    if form.is_valid():
        tamanioscantidades = TamaniosCantidades(tamanios=form.cleaned_data['tamanios'],
                            cantidades=form.cleaned_data['cantidades'])
        # tamanioscantidades = tamanioscantidades_form.save(commit = False)
        # tamanioscantidades.usuario = request.user
        tamanioscantidades.save()

    return HttpResponseRedirect('/')


def post_treasure(request):
    form = TreasureForm(request.POST)
    if form.is_valid():
        treasure = Treasure(name=form.cleaned_data['name'],
                            value=form.cleaned_data['value'])

        treasure.save()

    return HttpResponseRedirect('/')

1 个答案:

答案 0 :(得分:0)

您尚未初始化str数组。在定义row之后将其初始化:

int row = sheet.getRows();
str = new String[row];

这将摆脱NullPointerException。

但是,您也不创建二维数组。您正在创建一个一维数组,每个索引都包含一个仅包含最后一列数据的字符串。

稍微更改一下代码:

更改

String[] str;

收件人

String[][] str;

初始化rowcol后将其初始化:

int row = sheet.getRows();
int col = sheet.getColumns();
str = new String[row][col];

然后稍微改变一下循环:

for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++){
        Cell cell = sheet.getCell(j,i);
        String column;
        if (j == 5) {
            column = "Answer: " + cell.getContents();
        } else {
            column = cell.getContents().toString();
        }

        str[i][j] = column;
    }
}