Null 空检查中的分段错误?

Null 空检查中的分段错误?,null,segmentation-fault,Null,Segmentation Fault,我在尝试检查某些元素是否为NULL时遇到分段错误。有人能帮忙吗 void addEdge(int i, int j) { if (i >= 0 && j > 0) { Node* whereto; whereto = linkedAdjacencyList[i]; if(whereto != NULL) { while(whereto->a

我在尝试检查某些元素是否为
NULL
时遇到分段错误。有人能帮忙吗

    void addEdge(int i, int j) 
{
        if (i >= 0 && j > 0) 
    {
        Node* whereto;
        whereto = linkedAdjacencyList[i];
        if(whereto != NULL)
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[j];
        }
        else{linkedAdjacencyList[i]->adj = linkedAdjacencyList[j];}
        whereto = linkedAdjacencyList[j];
        if(whereto != NULL)
        {
            while(whereto->adj != NULL)
            {whereto = whereto->adj;}
            whereto->adj = linkedAdjacencyList[i];
        }
        else{linkedAdjacencyList[j]->adj = linkedAdjacencyList[i];}
            }
    }
救命啊

编辑:根据您的建议,这是新代码,但现在调用该方法时立即出现分段错误?我会把电话发出去

int edges;
in >> edges;
g.edges = edges;
for(int i = 0; i < edges; i++)
{
    int first;
    int second;
    in >> first >> second;
    g.addEdge(first, second);
}
int边;
在>>边缘;
g、 边=边;
对于(int i=0;i>第一>>第二;
g、 附录(第一、第二);
}

我建议
LinkedAjacyList[I]
返回
NULL
,因此您正在尝试取消引用
NULL
。只需为
NULL
添加额外检查:

Node* whereto = NULL;
whereto = linkedAdjacencyList[i];
if (NULL != whereto){
while(whereto->adj != NULL) .....

您似乎没有在
whereto=LinkedAdJacyList[i]中检查
whereto
是否正确初始化

表达式
到哪里->调整!=NULL
使用
whereto
作为指针,并在其与结构或类开头的偏移处使用
adj
解除对它的引用。如果该语句是segfaulting,则表示
whereto
的指针值不正确。检查linkedAdjancencyList[]中的内容,如果为NULL,则不要使用它。

在while循环上方进行if检查,并检查where to是否为NULL

if(whereto!=NULL){ while(whereto->adj!=NULL)。。。。。。。。。 }