打字稿功能上的对象可能为Null错误

时间:2020-06-26 00:28:07

标签: typescript algorithm function debugging null

嘿,我是新来输入script的人,在函数中得到一个Object可能为null错误。该函数应该接受两个表示数字的LinkedList(列表中的每个数字都是其自己的节点),并将其添加返回一个不同的Linked列表。有人可以向我解释为什么我会收到此错误吗?

这是错误

Line 33: Char 12: error TS2531: Object is possibly 'null'.
Line 33: Char 35: error TS2531: Object is possibly 'null'.
Line 35: Char 24: error TS2531: Object is possibly 'null'.
Line 36: Char 24: error TS2531: Object is possibly 'null'.

这是功能

// Definition for singly-linked list.
// class ListNode {
//    val: number
//    next: ListNode | null
//    constructor(val?: number, next?: ListNode | null) {
//        this.val = (val===undefined ? 0 : val)
//        this.next = (next===undefined ? null : next)
//    }
//}
//

function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
    let carry = 0;

    let cNode1 : ListNode | null = l1;
    let cNode2 : ListNode | null = l2;
    let returnNode : ListNode | null= new ListNode(0 , null); 
    let cNode3 : ListNode | null =  returnNode; 


    while(cNode3 != null){
        let cVal1 = 0; 
        let cVal2 = 0; 
        if(cNode1 != null)  cVal1 = cNode1.val;
        if(cNode2 != null)  cVal2 = cNode2.val; 
    
        let cVal = (cVal1 + cVal2 + carry);  
        let resultingVal = cVal % 10;
        carry = Math.floor(cVal / 10); 
        cNode3.val = resultingVal;
    
        if(cNode1.next != null || cNode2.next != null || carry > 0){
             cNode3.next  =  new ListNode(0 , null); 
             cNode1  = cNode1.next;
             cNode2  = cNode2.next;
        }
    
         cNode3 = cNode3.next;
    }

    return returnNode; 
};

第33行是以下语句:

if(cNode1.next != null || cNode2.next != null || carry > 0){

1 个答案:

答案 0 :(得分:0)

您在第33行中写道:

import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String s2=sc.nextLine();
        int n=sc.nextInt();
        String[] s3=s1.split(" ");
        String[] s4=s2.split(" ");
        int[] values=new int[s3.length];
        int[] weights=new int[s4.length];
        for(int i=0;i<s3.length;i++){             //extracting values from a string array
            values[i]=Integer.parseInt(s3[i]);
            weights[i]=Integer.parseInt(s4[i]);
        }
        int res=0;
        while(n>0){
            double max=-1;
            int maxi=0;
            for(int i=0;i<values.length;i++){
                if(weights[i]>0&&max<(double)weights[i]/values[i]){
                    max=(double)weights[i]/values[i];
                    maxi=i;
                }
            }
            if(weights[maxi]<=n){
                n=n-weights[maxi];
                res=res+values[maxi];
            }
            weights[maxi]=0;
            
        }
        System.out.println(res);
    }
}

的意思是,您尝试访问cNode1中的next,但是它可以为null。与cNode2相同。您应先检查它们是否为空,然后再检查其属性之一是否为空:

if(cNode1.next != null || cNode2.next != null || carry > 0){

或者,如果您的环境支持,则可以使用新的可选链接运算符(if((cNode1 && cNode1.next != null) || (cNode2 && cNode2.next != null) || carry > 0){ ),该运算符将检查对象是否为null或未定义,并且仅当它不是其中之一时,它才会尝试访问属性:

?.